A class has a similar type of method (let’s call it a collector). I think these methods can be a method by making it return a class wrapping generic values.
This is example code
public class Collector {
RestTemplate restTemplate = new RestTemplate();
/*I want to make method like this without making the Collector Class with generic value */
public APICallResultDto<T, V> collectData() {
APICallResultDto<T, V> result = new APICallResultDto<>();
/* boiler plate
* (1) complete request url
* (2) call api
* (3) validate response
* (4) transform data
* */
return result;
}
This code produce errors Cannot resolve symbol 'T' & Cannot resolve symbol 'V'
I know I can solve this problem by making Collector class with generic values. I want to Collector class to be singleton, I mean the bean. So I can’t do that.
Is there any way?
>Solution :
It will compile if you add <T, V> in front of return type, as shown below:
public <T, V> APICallResultDto<T, V> collectData() {
//...
}
For reference: Generic Methods