ClassCastException: RestTemplate returning List instead of List

Problem:

I have a POJO entity, say org.grassfield.Employee. I expose this as a REST services.

I consume this REST service,


List employeeList = restTemplate.getForObject(
"http://localhost:8080/api/v1/employees" ,
List.class);

Instead of getting Employee object, I’m getting LinkedHashMap. So my code fails with ClassCastException: RestTemplate returning List<LinkedHashMap> instead of List<Employee>

Solution:

Jackson uses LinkedHashMap to Serialize/De serialize the entity objects. When I consume the service using parameterized type reference, it works fine.


ResponseEntity<List> response = restTemplate.exchange(
"http://localhost:8080/api/v1/employees" ,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List>(){});
List EmployeeList = response.getBody();

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s