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