Can someone please tell me whether there is a way to pass the generic type T to getAPIResponse(...) and build the TypeReference within getAPIResponse method.
I would like to pass Model as a generic type to getAPIResponse and have TypeReference built in within the getAPIResponse method.
TypeReference<APIResponse<Model>> type = new TypeReference<APIResponse<Model>>(){};
APIResponse<Model> response = getAPIResponse(result, type);
I would like to avoid building the TypeReference instance outside the getAPIResponse method, rather would like to create the instance within the getAPIResponse method and pass only the Generic type into the method.
getAPIResponse method
protected final <T> APIResponse<T> getAPIResponse(MvcResult pResult, TypeReference<APIResponse<T>> type) {
APIResponse<T> res = null;
try {
res = new ObjectMapper().readValue(pResult.getResponse().getContentAsString(), type);
}
catch (IOException e) {
Assert.fail(ExceptionUtils.getStackTrace(e));
}
return res;
}
I tried changing to the below
protected final <T> APIResponse<T> getAPIResponse(MvcResult pResult, T t) {
APIResponse<T> res = null;
try {
TypeReference<APIResponse<T>> type = new TypeReference<APIResponse<T>>(){};
res = new ObjectMapper().readValue(pResult.getResponse().getContentAsString(), type);
}
catch (IOException e) {
Assert.fail(ExceptionUtils.getStackTrace(e));
}
return res;
}
But not I am not sure how to call into this method. Lets say I want to return
APIResponse<Bank>
APIResponse<Branch>
APIResponse<List<Employee>>
How do I pass these generic types into my method call in the invoking class?