Java 使用 Spring RestTemplate 发布带有对象的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25050617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:30:54  来源:igfitidea点击:

Using Spring RestTemplate to POST params with objects

javajsonspringresttemplate

提问by Matt Crysler

I am trying to send a POST request using Spring's RestTemplate functionality but am having an issue sending an object. Here is the code I am using to send the request:

我正在尝试使用 Spring 的 RestTemplate 功能发送 POST 请求,但在发送对象时遇到问题。这是我用来发送请求的代码:

RestTemplate rt = new RestTemplate();

MultiValueMap<String,Object> parameters = new LinkedMultiValueMap<String,Object>();
parameters.add("username", usernameObj);
parameters.add("password", passwordObj);

MyReturnObj ret = rt.postForObject(endpoint, parameters, MyRequestObj.class);

I also have a logging interceptor so I can debug the input parameters and they are almostcorrect! Currently, the usernameObjand passwordObjparameters appear as such:

我还有一个日志拦截器,所以我可以调试输入参数,它们几乎是正确的!目前,usernameObjpasswordObj参数如下所示:

{"username":[{"testuser"}],"password":[{"testpassword"}]}

What I wantthem to look like is the following:

希望它们看起来如下:

username={"testuser"},password={"testpassword"}

Assume that usernameObjand passwordObjare Java objects that have been marshalled into JSON.

假设usernameObjpasswordObj是已编组为 JSON 的 Java 对象。

What am I doing wrong?

我究竟做错了什么?

采纳答案by Matt Crysler

Alright, so I ended up figuring this out, for the most part. I ended up just writing a marshaller/unmarshaller so I could handle it at a much more fine grained level. Here was my solution:

好吧,所以我最终弄清楚了这一点,在很大程度上。我最终只写了一个编组器/解组器,所以我可以在更细粒度的级别处理它。这是我的解决方案:

RestTemplate rt = new RestTemplate();

// Create a multimap to hold the named parameters
MultiValueMap<String,String> parameters = new LinkedMultiValueMap<String,String>();
parameters.add("username", marshalRequest(usernameObj));
parameters.add("password", marshalRequest(passwordObj));

// Create the http entity for the request
HttpEntity<MultiValueMap<String,String>> entity =
            new HttpEntity<MultiValueMap<String, String>>(parameters, headers);

// Get the response as a string
String response = rt.postForObject(endpoint, entity, String.class);

// Unmarshal the response back to the expected object
MyReturnObj obj = (MyReturnObj) unmarshalResponse(response);

This solution has allowed me to control how the object is marshalled/unmarshalled and simply posts strings instead of allowing Spring to handle the object directly. Its helped immensely!

这个解决方案使我能够控制对象的编组/解组方式,并简单地发布字符串,而不是让 Spring 直接处理对象。它的帮助非常大!

回答by Zaw Than oo

For Client Side

对于客户端

To pass the object as json string, use MappingHymanson2HttpMessageConverter.

要将对象作为 json 字符串传递,请使用MappingHymanson2HttpMessageConverter.

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingHymanson2HttpMessageConverter());

For Server Sidespring configuration

对于服务器端弹簧配置

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
        </list>
    </property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter"/>