50

I'm trying to access getter methods on my MyModelClass but my code is returning List<LinkedHashMap> instead of List<MyModelClass>. This is my code.

List<MyModelClass> myModelClass = 
   (List<MyModelClass>) restTemplate.postForObject(url,mvm,List.class);

System.out.println("Response= " +  myModelClass);

I tried to print the response and I got the JSON Response that I'm expecting. but when I tried to run this code.

System.out.println("Response= " +  myModelClass.get(0).getMessage());

It will produce this error.

java.lang.ClassCastException: 
java.util.LinkedHashMap cannot be cast to com.XXX.XXX.MyModelClass 

It is a mismatch. Can someone help me to get rid with this error? thanks.

MyModelClass

public class MyModelClass{
        
    /**
     * 
     */
    @JsonProperty("id")
    public String id;
    
    @JsonProperty("type")
    public String type;
    
    @JsonProperty("user")
    public String user;
    
    @JsonProperty("message")
    public String message;

    //getters

Error for

MyModelClass[] myModelClass= restTemplate.postForObject(url,mvm, myModelClass[].class);

org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: 
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

JSON Response Structure

    [{"key1":"value1","key2":"value2","parameters":{"key1":"value1","key2":"value2","key3":"value3","key4":"value4","key5":"value5"}},
{"key12":"value12","key22":"value22","parameters":{"key12":"value12","key22":"value22","key32":"value32","key42":"value42","key52":"value52"}}]

If there is any suggestion on how to map this kind of JSON Response in RestTemplate,It would help a lot. thanks

3
  • 1
    Hi Erick, have you found any solution for your problem? I have almost same problem too Commented Apr 21, 2014 at 7:32
  • 1
    This answer helped me out to solve the issue. stackoverflow.com/a/15453159/822436 Commented Mar 9, 2015 at 8:57
  • There is some way to convert List<LinkedHashMap> to List<MyModelClass>. But why it doesn't throw any exception when restTemplate retruns List<LinkedHashMap> and assgin it to myModelClass[]? They are differnt types. Why? Commented Jul 24, 2019 at 2:14

2 Answers 2

66

With the following method call

List<MyModelClass> myModelClass=(List<MyModelClass>) restTemplate.postForObject(url,mvm,List.class);

All Jackson knows is that you want a List, but doesn't have any restriction on the type. By default Jackson deserializes a JSON object into a LinkedHashMap, so that's why you are getting the ClassCastException.

If your returned JSON is an array, one way to get it is to use an array

MyModelClass[] myModelClasses = restTemplate.postForObject(url,mvm, MyModelClass[].class);

You can always add the elements from that array to a List.

I can't remember since what version, but RestTemplate#exchange now has an overload that accepts a ParameterizedTypeReference argument. The ParameterizedTypeReference is the type token hack for suggesting a parameterized type as the target for deserialization.

You can refactor the code above to use exchange instead of postForObject, and use ParameterizedTypeReference to get a List<MyModelClass>. For example

ParameterizedTypeReference<List<MyModelClass>> typeRef = new ParameterizedTypeReference<List<MyModelClass>>() {
};
ResponseEntity<List<MyModelClass>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(mvm), typeRef);
List<MyModelClass> myModelClasses = responseEntity.getBody();
Sign up to request clarification or add additional context in comments.

Comments

3

With the following method call , you can convert LinkedHashMap to List

ObjectMapper mapper = new  ObjectMapper();
List<Object> list = mapper.convertValue(listResponse, new TypeReference<List<Object>>() {})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.