简体   繁体   English

SPRING:无法使用rest模板在发布请求中传递参数

[英]SPRING: Unable to pass parameters in a post request using rest template

I am using Spring's Rest Template to post a request with data to a web service. 我正在使用Spring的Rest Template将带有数据的请求发布到Web服务。 Here is my client code snippet- 这是我的客户代码段-

        RestTemplate restTemplate = new RestTemplate();

        MultiValueMap<String, RequestDefinition> map = new LinkedMultiValueMap<String, RequestDefinition>();
        map.add("requestJSON", someDataObject);

        MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
        restTemplate.getMessageConverters().add(jsonHttpMessageConverter);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        restTemplate.postForObject(someUrl, map, String.class);

Here is my servet code post method- 这是我的服务代码发布方法-

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String reqJSONStr = (String) request.getParameter("requestJSON");

          // reqJSONStr is coming as null. No idea why  
          // some other logic follows here
    }

The problem is, in my servlet post method, the requestJSON object is coming as null. 问题是,在我的servlet post方法中,requestJSON对象为null。 Can somebody please point out the mistake I am doing here. 有人可以指出我在这里犯的错误吗?

Copying from my comment: 从我的评论中复制:

I'd guess your data is on the request body in json format, somewhere inside request.getReader()... 我猜你的数据以json格式放在请求正文中,位于request.getReader()内的某处...

As how to pass it as parameter, if your object is complex (has another objects inside as fields) I don't think you can pass all of it as url parameters. 至于如何将其作为参数传递,如果您的对象很复杂(内部还有其他对象作为字段),我认为您不能将所有参数都作为url参数传递。 If not, try something like this if you HAVE TO send your object as url parameters. 如果没有,如果您必须将对象作为url参数发送,请尝试这样的操作。 You'd also need to read your object in your servlet piece by piece. 您还需要逐步读取servlet中的对象。

RestTemplate rt = new RestTemplate();

rt.setMessageConverters(
        Arrays.asList(new HttpMessageConverter[]{new FormHttpMessageConverter(), new StringHttpMessageConverter()}));

MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("some", someDataObject.getSomeProperty().toString());
map.add("another", someDataObject.getAnotherProperty().toString());

String resp = rt.postForObject("http://scooterlabs.com/echo", map, String.class);

System.out.println(resp);

As you can see, there isn't a HttpMessageConverter that can convert an object to url encoded parameters, so when you put MappingJackson2HttpMessageConverter into list of converters, and try to add an object to request, it will be converted to JSON and end up in the request BODY. 如您所见,没有一个HttpMessageConverter可以将对象转换为url编码的参数,因此,当您将MappingJackson2HttpMessageConverter放入转换器列表中,并尝试添加要请求的对象时,它将被转换为JSON并最终以请求身体。

Personally I'd change my servlet code to something that can read JSON from body. 我个人将servlet代码更改为可以从正文读取JSON的代码。 JSON is a better choice if you are trying to send a complex object with lots of properties than url encoded form. 如果您尝试发送具有比url编码形式更多属性的复杂对象,则JSON是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM