来自复杂对象的spring restTemplate POST参数 [英] spring restTemplate POST parameters from complex object

查看:159
本文介绍了来自复杂对象的spring restTemplate POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to test our REST service using restTemplate using the postForObject(...) method.

单元测试:

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

<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-form-urlencoded.

I understand that the FormHttpMessageConverter will convert to and from MultiValueMap and media type application/x-www-form-urlencoded.

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

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?

我的服务器希望获取看起来像这样的POST参数:

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

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转换为参数列表.
这是服务器端控制器方法的签名:

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:

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

推荐答案

您可以将RestTemplate与消息转换器一起使用.我已经对其进行了测试

You can use RestTemplate with message converters. I have tested it and it works

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

            restTemplate.postForEntity(url, requestBodyObject, returnTypeClass);

这篇关于来自复杂对象的spring restTemplate POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆