So I'm trying to create a method which will return a List of any type. This is my method:
public <T> List<T> mapResponse(Response response){
List<T> list = new ArrayList<>();
//String responseString;
if (response != null) {
String responseString = response.getBody();
if (response.getStatusCode() == 200) {
list = objectMapper.readValue(responseString, new TypeReference<List<T>>() {
});
} else {
log.error("Response status code: {} Response : {}", response.getStatusCode(), responseString);
throw new BadRequestException(responseString);
}
} else {
log.error("Response from service is null");
throw new InternalServerErrorException(ResponseIsNull);
}
return list;
}
When I try to iterate trough the list I get java.lang.ClassCastException.
List<Transaction> list = mapResponse(Response response);
for (Transaction transaction : list) {
log.info(transaction.toString()); }
How can I solve this?
Thanks!