Skip to content
Advertisement

Generic restTemplateExchange with generic responseType

I would like to create a own generic method that calls the restTemplate.exchange. In particular I would like that the signature of this generic method to be :

protected <T> ResponseEntity<T> genericExchange(String uri, HttpMethod method, @Nullable HttpEntity<?> requestEntity, -->responseType<--)

I would differentiate what exchange method call according to “responseType”

  • if responseType is Class< T> I call * exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class< T> responseType)*
  • if responseType is ParameterizedTypeReference< T> I call * exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, ParameterizedTypeReference< T> responseType)*

Advertisement

Answer

I found this answer, and it was working for me:

Unable to get a generic ResponseEntity<T> where T is a generic class “SomeClass<SomeGenericType>”

RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange(pUrl, HttpMethod.POST, pEntity, new ParameterizedTypeReference<ResultListPage<MyObject>>() {});
Advertisement