ParameterizedTypeReference
spring
ParameterizedTypeReference
The purpose of this class is to enable capturing and passing a generic Type. In order to capture the generic type and retain it at runtime, you need to create a subclass (ideally as anonymous inline class) as follows:
ParameterizedTypeReference<List<String>> typeRef = new ParameterizedTypeReference<List<String>>() {};
Here is an example of how you might use ParameterizedTypeReference with Spring's RestTemplate class:
// Create a ParameterizedTypeReference object that captures the generic type List<MyObject>
ParameterizedTypeReference<List<MyObject>> typeRef = new ParameterizedTypeReference<List<MyObject>>() {};
// Make a GET request to an API that returns a List<MyObject>
ResponseEntity<List<MyObject>> response = restTemplate.exchange(url, HttpMethod.GET, null, typeRef);
// Get the list of MyObject instances from the response
List<MyObject> myObjects = response.getBody();
Use ParameterizedTypeReference with Spring's WebClient class:
// Return type is Mono<Page<OrderSpec.SearchResponse>>
return this.webClient.get()
.uri(builder.build().toUri())
.retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse -> handleClientError(clientResponse))
.onStatus(HttpStatus::is5xxServerError, clientResponse -> handleServerError(clientResponse))
.bodyToMono(new ParameterizedTypeReference<Page<OrderSpec.SearchResponse>>() {})
.onErrorMap(throwable -> {
if (throwable instanceof WebClientResponseException) {
WebClientResponseException ex = (WebClientResponseException) throwable;
return new ServiceException(ex.getResponseBodyAsString());
}
return throwable;
});