I have a candy factory that exposed various endpoints to get candies in a bag.
candy such as
- name: m&m ; endpoint: candy-factory/mnms
- name: skittle; endpoint: candy-factory/skittles
- name: smarties; endpoint: candy-factory/smarties
Api response pattern is below
public class CandyWrapper <T> {
@JsonProperty("candies")
private List<T> candies;
}
I have a generic service class which deals with getting the candies using Spring RestTemplate
public class CandyStore<T> {
private final RestTemplate restTemplate;
private final Class<CandyWrapper<T>> type
String candyFactroyUrl;
public CandyStore(Class<CandyWrapper<T>> type, RestTemplate restTemplate, String
factoryUrl) {
this.type = type;
....
....
}
public CandyWrapper<T> getCandies(){
HttpEntity<HttpHeaders> request = new HttpEntity<>();
ResponseEntity<CandyWrapper<T>> response;
response = restTemplate.exchange(candyFactroyUrl, HttpMethod.GET, request, type);
return response.getBody();
}
}
Now when I am going to configure the CandyStore service with the actual type of the Candy ; Unable to determine the candy type to pass in the service constructor.
@Bean()
public getSkittleCandyStore(RestTemplate restTemplate, String skittleUrl){
//TODO
get the class of type CandyWrapper<Skittle> so that I can pass it to the service
constructor.
CandyWrapper<Skittle> instance = new CandyWrapper<Skittle>();
// instance.getClass() does not give the class conforming to the parameter below.
return new CandyStore(???, restTemplate, skittleUrl);
}
Any help to clear up the generics concepts is appreciated. Thanks!!