Java 内容类型';空';Spring RESTTemplate getForObject方法返回的不受支持

Java 内容类型';空';Spring RESTTemplate getForObject方法返回的不受支持,java,rest,http,spring-mvc,Java,Rest,Http,Spring Mvc,我在Spring MVC控制器中有一个简单的REST方法,其签名如下: @RequestMapping(value=“/person/{personId}”,method=RequestMethod.GET) public@ResponseBody对象getPerson(@PathVariable(“personId”)字符串personId){ ... } 输出为typeObject,因为此方法返回了几种不同的数据类型 从Spring MVC应用程序内的测试程序调用时,如下所示: privat

我在Spring MVC控制器中有一个简单的REST方法,其签名如下:

@RequestMapping(value=“/person/{personId}”,method=RequestMethod.GET) public@ResponseBody对象getPerson(@PathVariable(“personId”)字符串personId){
... }

输出为type
Object
,因为此方法返回了几种不同的数据类型

从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(); 
    ...
    }
响应
内容类型“null”不受支持
,调用失败

有人能告诉我为什么吗

当从另一个不使用Spring但发出HTTP GET请求的应用程序中的测试程序调用时,控制器方法被正确调用并工作。

您可以尝试以下方法:

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);
如果上述方法无效,您可以尝试使用
实体
方法,如:

  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);
  }
RestTemplate RestTemplate=new RestTemplate();
//准备可接受的介质类型
List acceptableMediaTypes=new ArrayList();
acceptableMediaTypes.add(MediaType.APPLICATION_XML);//设定你需要的
//准备收割台
HttpHeaders=新的HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity=新的HttpEntity(标题);
//将请求作为GET发送
试一试{
ResponseEntity结果=restTemplate.exchange(“http://localhost:8080/Library/rest/person/1,HttpMethod.GET,entity,PersonList.class);
//添加到模型
model.addAttribute(“persons”,result.getBody().getData());
}捕获(例外e){
错误(e);
}
有很多


希望能提供帮助

谢谢,但是关于第一个问题,版本检查、读取、超时在哪里?对于第二个,参数(“”)会引起麻烦。@MrMorgan抱歉,请再次检查。我在
Person
类中没有这样的
getData()
方法。然而,尽管如此,代码仍然返回不支持的内容类型“null”,这很奇怪,因为我已经将媒体类型设置为JSON