Announcement Announcement Module
Collapse
No announcement yet.
spring restTemplate POST parameters from complex object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring restTemplate POST parameters from complex object

    I'm attempting to test our REST service using restTemplate using the postForObject(...) method. I need to send POST parameters, but my request insists on POSTing XML to the service. What am I missing?

    unit test:
    Code:
    	
        @Test
        	public void testPostOrder() {
        		String url = BASE_URL + "/orders/";
        		OrderDto orderDtoInput = new OrderDto();
        		orderDtoInput.setCustomerId(34);
        
        
        		UpdateReportDto updateReport = restTemplate.postForObject(url,
        				orderDtoInput, UpdateReportDto.class, new Object[] {});
                }
    the interesting piece of my configuration, test-rest-servlet.xml:

    Code:
    	<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    		<property name="messageConverters">
    			<list>
    				<ref bean="formHttpMessageConverter" />
    				<ref bean="marshallingHttpMessageConverter" />
    			</list>
    		</property>
    	</bean>
    	
    	<bean id="formHttpMessageConverter" class="org.springframework.http.converter.FormHttpMessageConverter">
    	</bean>
    I understand that the FormHttpMessageConverter will convert to and from MultiValueMap<String, String> and media type
    application/x-www-form-urlencoded.

    Is there any magic, or tools I can use or wire in to convert my Dto to a MultiValueMap ??? or do I need to cycle over the object properties and build my own MultiValueMap in my test? Regardless of any configuration changes i make in my test-rest-servlet.xml the request always POSTs XML data.

    my server is expecting to get POST parameters that look something like this:

    Code:
       id=11752&firstName=Joe&active=true&address1=1122&address2=2233&c
        ellPhone=123-321-1234&childrensName1=bobby1&childrensName2=bobby2&childrensName3=bobby3&childrensName4=bobby4&city=someCity&
        customHobbies=loves To Fly Planes&distributorId=407&email=[email protected]&fax=321-123-1234&fellowship=good fellows&fishing=false&golf=true&hunting=false&
        insuranceCompany1=ins1&insuranceCompany2=ins2&insuranceCompany3=ins3&insuranceCompany4=ins4&lastName=Brownie&
        mailMerge=true&medicalSchool=Granada U&officeDays=4&officeManager=manager&officeManagerPhone=456.654.4567&other=true&
        paNurse=nurse 1&paNursePhone=345-543-3456&
        phone=234-432-2345&
        salesRepresentativeId=1935&specialty=meatball surgery&spouseName=Betty&state=AL&
        surgeryDays=22&title=doc&version=2&zip=47474
        promptValues[0].id=12&promptValues[0].miscPromptId=882&promptValues[0].value=meFirst&
        promptValues[1].id=13&promptValues[1].miscPromptId=881&promptValues[1].value=youToo&residency=Jamaica General&
        surgeonClinics[0].address1=newAddress&surgeonClinics[0].address2=newAddress2&surgeonClinics[0].city=clinic  City&
        surgeonClinics[0].email=[email protected]&surgeonClinics[0].fax=123.456.7890&surgeonClinics[0].id=33273&
        surgeonClinics[0].name=clinic name&surgeonClinics[0].phone=890-098-4567&
        surgeonClinics[0].zip=34567&surgeonClinics[0].surgeryCenter1=MySurgeryCenter1&
        surgeonClinics[0].surgeryCenter2=MySurgeryCenter2&
        surgeonClinics[1].address1=newAddress11&surgeonClinics[1].address2=newAddress22&surgeonClinics[1].city=clinic2 City&
        surgeonClinics[1].email=[email protected]&surgeonClinics[1].fax=123.456.7890&surgeonClinics[1].id=33274&
        surgeonClinics[1].name=clinic2 name&surgeonClinics[1].phone=890-098-4567&
        surgeonClinics[1].zip=34567&
        surgeonClinics[1].surgeryCenter1=MySurgeryCenter21&surgeonClinics[1].surgeryCenter2=MySurgeryCenter22&
    Here's what I don't get: our RestServiceController method knows how to take this crazy parameter list and re-create our Dto object. We can successfully call it using curl. It seems that some reciprocal magic should exist on the client side to turn the Dto into the parameter list.
    Here's the signature of the server side controller method:

    Code:
        // createOrder
        	@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT }, value = "/orders/")
        	@ResponseBody
        	public UpdateReportDto createOrder(OrderDto orderDto,
        			HttpServletRequest httpServletRequest,
        			HttpServletResponse httpServletResponse) {
Working...
X