Generics with Spring RESTTemplate Generics with Spring RESTTemplate java java

Generics with Spring RESTTemplate


ParameterizedTypeReference has been introduced in 3.2 M2 to workaround this issue.

Wrapper<Model> response = restClient.exchange(loginUrl,                           HttpMethod.GET,                           null,                           new ParameterizedTypeReference<Wrapper<Model>>() {}).getBody();

However, the postForObject/getForObject variant was not introduced.


The only thing I think you could do is creating a new class that extends Wrapper and uses model as a generic.

class WrapperWithModel extends Wrapper<Model>{};WrapperWithModel response = restTemplate.getForObject(URL, WrapperWithModel.class);

It's not the best solution, but at least you won't have to unmarshall manually the response.


Do not use generics with RestTemplate. Wrap request and response object with wrapper object that will hide the generics.