json 需要有关 RestTemplate postForObject() 方法的帮助
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20113743/
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
Need help on RestTemplate postForObject() method
提问by omjaijagdish
I have to send JSON data from one service method to the other using postForObject()method.
I saw one example on RestTemplateon this link.
我必须将 JSON 数据从一种服务方法发送到另一种使用postForObject()方法。我RestTemplate在这个链接上看到了一个例子。
postForObject()method has the following format:
postForObject()方法具有以下格式:
User returns = rt.postForObject(uri, u, User.class, vars);
Or
或者
User returns = rt.postForObject(uri, u, User.class);
I want to know that, after using postForObject()method, if we implement the service method to accept the Userobject, how it will look like?
我想知道,在使用postForObject()方法之后,如果我们实现服务方法来接受User对象,它会是什么样子?
In my project, I have code like
在我的项目中,我有这样的代码
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingHymansonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
String uri = "http://testcode.com/myapp/api/launchservices";
ServiceRequest request = new ServiceRequest();
request.setId(UUID.randomUUID().toString());
....
I am getting error at this line:
我在这一行遇到错误:
ServiceRequest req = restTemplate.postForObject(uri, request, ServiceRequest.class);
while executing this, I am getting this error mesage:
执行此操作时,我收到此错误消息:
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:537)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:493)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:302)
my implementation method is:
我的实现方法是:
@RequestMapping(value = "/launchservices", method = RequestMethod.POST)
@ResponseBody
public boolean launchServices(@PathVariable ServiceRequest request) {
System.out.println("Request: "+request.toString());
return true;
}
How to get rid of this? What will be the URI?
如何摆脱这种情况?URI 是什么?
采纳答案by omjaijagdish
I got solution to this problem.
我得到了这个问题的解决方案。
In this example,method postForObject returns an object of class "ServiceRequest"
在这个例子中,方法 postForObject 返回一个类“ServiceRequest”的对象
ServiceRequest req = restTemplate.postForObject(uri, request, ServiceRequest.class);
So, the method that implements this service with the above 'uri'should return an object of class ServiceRequest
All it needs is, slight modification in implementation method as below
所以,用上面的“uri”实现这个服务的方法应该返回一个ServiceRequest类的对象
它所需要的只是,对实现方法稍作修改如下
@RequestMapping(value = "/launchservices", method = RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
public ServiceRequest launchServices(@RequestBody ServiceRequest request) {
System.out.println("Request: "+request.toString());
return request;
}

