一个抽象控制器类需要从REST获取对象列表。使用Spring RestTemplate时,它没有将其映射到所需的类,而是返回了Linked HashMap。
public List<T> restFindAll() {
RestTemplate restTemplate = RestClient.build().restTemplate();
ParameterizedTypeReference<List<T>> parameterizedTypeReference = new ParameterizedTypeReference<List<T>>(){};
String uri= BASE_URI +"/"+ getPath();
ResponseEntity<List<T>> exchange = restTemplate.exchange(uri, HttpMethod.GET, null,parameterizedTypeReference);
List<T> entities = exchange.getBody();
// here entities are List<LinkedHashMap>
return entities;
}
如果我使用,
ParameterizedTypeReference<List<AttributeInfo>> parameterizedTypeReference =
new ParameterizedTypeReference<List<AttributeInfo>>(){};
ResponseEntity<List<AttributeInfo>> exchange =
restTemplate.exchange(uri, HttpMethod.GET, null,parameterizedTypeReference);
它运行良好,但不能放在所有子类中,是否有其他解决方案。
return restTemplate.exchange(uri, HttpMethod.GET, null, responseType).getBody(); }``` 调用方式相同 调用方式为 List dtoList = this.exchangeAsList("http://my/url", new ParameterizedTypeReference<>() {});
- Mrinal Bhattacharjee