Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RestTemplate and generic types ParameterizedTypeReference collections like List<T>

An Abstract controller class requires List of objects from REST. While using Spring RestTemplate its not mapping it to required class instead it returns 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;  } 

If I use,

ParameterizedTypeReference<List<AttributeInfo>>  parameterizedTypeReference =      new ParameterizedTypeReference<List<AttributeInfo>>(){};     ResponseEntity<List<AttributeInfo>> exchange =   restTemplate.exchange(uri, HttpMethod.GET, null,parameterizedTypeReference); 

It works fine. But can not put in all subclasses, any other solution.

like image 843
vels4j Avatar asked Apr 28 '16 13:04

vels4j


People also ask

What is ParameterizedTypeReference in spring?

ParameterizedTypeReference<List<String>> typeRef = new ParameterizedTypeReference<List<String>>() {}; The resulting typeRef instance can then be used to obtain a Type instance that carries the captured parameterized type information at runtime.

What are Resttemplate methods?

Rest Template is used to create applications that consume RESTful Web Services. You can use the exchange() method to consume the web services for all HTTP methods. The code given below shows how to create Bean for Rest Template to auto wiring the Rest Template object.

What is Resttemplate postForEntity?

The postForEntity method creates new resource by posting the given object to the given URI template using HTTP POST method. The postForEntity method returns instance of ResponseEntity using which we can fetch the information about HTTP status, URI of newly created resource, response content body etc.

What is Resttemplate getForObject?

The getForObject method fetches the data for the given response type from the given URI or URL template using HTTP GET method. To fetch data for the given key properties from URL template we can pass Object Varargs and Map to getForObject method. The getForObject returns directly the object of given response type.


1 Answers

I worked around this using the following generic method:

public <T> List<T> exchangeAsList(String uri, ParameterizedTypeReference<List<T>> responseType) {     return restTemplate.exchange(uri, HttpMethod.GET, null, responseType).getBody(); } 

Then I could call:

List<MyDto> dtoList = this.exchangeAsList("http://my/url", new ParameterizedTypeReference<List<MyDto>>() {}); 

This did burden my callers with having to specify the ParameterizedTypeReference when calling, but meant that I did not have to keep a static mapping of types like in vels4j's answer 

like image 66
Rossiar Avatar answered Oct 03 '22 10:10

Rossiar