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

发布于 2024-09-27 00:57:23 字数 4102 浏览 2 评论 0原文

我正在尝试使用 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-form-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 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 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 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&

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

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

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

unit test:

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

I understand that the FormHttpMessageConverter will convert to and from MultiValueMap 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?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2024-10-04 00:57:23

您可以将 RestTemplate 与消息转换器一起使用。我已经测试过了并且有效

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

            restTemplate.postForEntity(url, requestBodyObject, returnTypeClass);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文