【发布时间】:2017-02-26 00:06:18
【问题描述】:
我想用Spring的RestTemplate做一个服务,在我的服务端代码是这样的:
@PostMapping(path="/savePersonList")
@ResponseBody
public List<Person> generatePersonList(@RequestBody List<Person> person){
return iPersonRestService.generatePersonList(person);
}
如果我使用此代码调用服务,则在客户端:
List<Person> p = (List<Person>) restTemplate.postForObject(url, PersonList, List.class);
我不能将p 对象用作List<Person>,它将变成LinkedHashList。
经过一番研究,我找到了一个解决方案,说我必须使用交换方法调用服务:
ResponseEntity<List<Person>> rateResponse = restTemplate.exchange(url, HttpMethod.POST, personListResult, new ParameterizedTypeReference<List<Person>>() {});
并且使用此解决方案服务器无法获取对象并引发异常,正确的方法是什么?
【问题讨论】:
-
正确的方法是,
@AutowiredRestTemplate 类并使用它而不是每次都创建新对象.. -
我认为你不明白我的问题
-
然后发布堆栈跟踪...
-
请显示异常和完整的堆栈跟踪
-
我遇到了同样的问题,我使用objectMapper从模型转换为字符串
标签: java spring spring-boot resttemplate