RestTemplate:如何获得通用列表响应 [英] RestTemplate: how to get generic List response
问题描述
我们正在为我们的应用程序使用公共包中的 restemplate.所以我们需要使用泛型.
We are using restemplate in a commons package for our application. So we need to use generic types.
我阅读了很多关于此的解决方案,但似乎没有一个对我们不起作用,而且我们不断得到(在客户端):
I read many solutions about that, but none seems to not work for us, and we constantly get (on the client side):
java.util.LinkedHashMap cannot be cast to nc.gouv.dsf.ranch.model.Pays
这是代码(总结):
public List<T> findAll(C criteria) {
[...]
ResponseEntity<List<T>> response =
restTemplateFactory.getRestTemplate().exchange(
url,
HttpMethod.GET,
new HttpEntity<>(createHttpHeaders(srvId)),
new ParameterizedTypeReference<List<T>>() {}
);
return response.getBody();
}
我虽然 ParameterizedTypeReference 可以解决此类问题,但它不起作用.
I though ParameterizedTypeReference what the solution to this kind of problems but its doesn't work.
PS:我们使用的是 springboot 1.3.1.RELEASE
PS: we are using springboot 1.3.1.RELEASE
推荐答案
似乎请求正在返回一个 Map 并且我们正在尝试将其转换为列表,因此出现异常.您可以尝试以下操作:
It seems the request is returning a Map and we are trying to cast it into list, hence the exception. Can you try the following:
ResponseEntity<Map<String, Object>> response =
restTemplateFactory.getRestTemplate().exchange(
url,
HttpMethod.GET,
new HttpEntity<>(createHttpHeaders(srvId)),
new ParameterizedTypeReference<Map<String, Object>>() {}
);
这篇关于RestTemplate:如何获得通用列表响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!