spring restTemplate来自复杂对象的POST参数

时间:2010-10-13 21:24:03

标签: spring rest junit

我正在尝试使用postForObject(...)方法使用restTemplate测试我们的REST服务。

单元测试:

@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[] {});
        }

我配置的有趣部分:

<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>

据我所知,FormHttpMessageConverter将与MultiValueMap和媒体类型进行转换 应用程序/ x-WWW窗体-urlencoded。

是否有任何魔法或工具可以使用或连接将我的Dto转换为MultiValueMap ???或者我是否需要遍历对象属性并在我的测试中构建自己的MultiValueMap?

我的服务器期望得到类似这样的POST参数:

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=doc@surgeon.com&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@clinic1.com&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@clinic2.com&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&

这是我没有得到的:我们的RestServiceController方法知道如何获取这个疯狂的参数列表并重新创建我们的Dto对象。我们可以使用curl成功调用它。似乎客户端应该存在一些互惠魔法来将Dto转换为参数列表 这是服务器端控制器方法的签名:

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

1 个答案:

答案 0 :(得分:0)

您可以将RestTemplate与消息转换器一起使用。我测试了它并且它可以工作

            RestTemplate restTemplate = new RestTemplate();
            List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
            messageConverters.add(new MappingJackson2HttpMessageConverter());
            restTemplate.setMessageConverters(messageConverters);

            restTemplate.postForEntity(url, requestBodyObject, returnTypeClass);