Java Spring RESTTemplate getForObject 方法不支持返回的内容类型“null”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24501590/
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
Content type 'null' not supported returned by Spring RESTTemplate getForObject method
提问by Mr Morgan
I have a simple REST method in a Spring MVC controller as follows which has a signature of:
我在 Spring MVC 控制器中有一个简单的 REST 方法,如下所示,其签名为:
@RequestMapping(value="/person/{personId}", method=RequestMethod.GET) public @ResponseBody Object getPerson(@PathVariable("personId") String personId) {
... }
@RequestMapping(value="/person/{personId}", method=RequestMethod.GET) public @ResponseBody Object getPerson(@PathVariable("personId") String personId) {
... }
The output is type Object
because several different data types are returned from this method.
输出是类型,Object
因为此方法返回了几种不同的数据类型。
When called from a test program within the Spring MVC application, as follows:
从 Spring MVC 应用程序中的测试程序调用时,如下所示:
private static void getPerson() {
logger.info(Rest.class.getName() + ".getPerson() method called.");
RestTemplate restTemplate = new RestTemplate();
Person person = restTemplate.getForObject("http://localhost:8080/Library/rest/person/1", Person.class);
ObjectMapper responseMapper = new ObjectMapper();
...
}
The response is Content type 'null' not supported
and the call fails.
响应是Content type 'null' not supported
并且调用失败。
Can anyone advise why?
谁能建议为什么?
When called from a test program in another application which doesn't use Spring but which makes HTTP GET requests, the controller method is called properly and works.
当从另一个不使用 Spring 但发出 HTTP GET 请求的应用程序中的测试程序调用时,控制器方法被正确调用并工作。
采纳答案by Federico Piazza
You can try this:
你可以试试这个:
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setReadTimeout(VERSION_CHECK_READ_TIMEOUT);
RestTemplate template = new RestTemplate(requestFactory);
Person person = restTemplate.getForObject("http://localhost:8080/Library/rest/person/1", Person.class);
If above doesn't work you can try using a Entity
approach like:
如果上述方法不起作用,您可以尝试使用以下Entity
方法:
RestTemplate restTemplate = new RestTemplate();
// Prepare acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_XML); // Set what you need
// Prepare header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<Person> entity = new HttpEntity<Person>(headers);
// Send the request as GET
try {
ResponseEntity<PersonList> result = restTemplate.exchange("http://localhost:8080/Library/rest/person/1", HttpMethod.GET, entity, PersonList.class);
// Add to model
model.addAttribute("persons", result.getBody().getData());
} catch (Exception e) {
logger.error(e);
}
There are many useful examples here.
这里有很多有用的例子。
Hope to help
希望有所帮助