My question is complete related to understand better the techniques to work with Json and check if I can do the way I was thinking as better practice. I am returning an ArrayList<myPojo>
insinde the response from the rest web service and I can't understand why it always return it as LinkedHashMap
even though I typed ArrayList<myPojo>
in the model object I am using. Is there a way to return a ArrayList<myPojo>
from rest web service or I really have to convert from LinkedHasMap
to the expected object?
For instance, is there a way, maybe by using @Entity or some other annotation, to avoid the conversion below? The closest question I found in this fórum to my question is ClassCastException: RestTemplate returning List<LinkedHashMap> instead of List<MymodelClass> but it didn't answer the question if I can avoid the converter in client side and return an ArrayList<myPojO>
- one reason because he is not declaring the webservice as I am and in this case @GenericArguments(RedemptionDetail.class)
, RedemptionDetail
is the exactly response and in my case the ArrayList
is part of a more complex object (LogDisplay
). I pasted the rest webservice relevant points below.
//the jars list
"aopalliance-1.0.jar"
"commons-logging-1.1.1.jar"
"hibernate-validator-4.1.0.Final.jar"
"hibernate-validator-annotation-processor-4.1.0.Final.jar"
"jackson-annotations-2.2.3.jar"
"jackson-core-2.2.3.jar"
"jackson-databind-2.2.3.jar"
"slf4j-api-1.7.5.jar"
"slf4j-log4j12-1.7.5.jar"
"spring-aop-4.1.2.RELEASE.jar"
"spring-beans-4.1.2.RELEASE.jar"
"spring-context-4.1.2.RELEASE.jar"
"spring-context-support-4.1.2.RELEASE.jar"
"spring-core-4.1.2.RELEASE.jar"
"spring-expression-4.1.2.RELEASE.jar"
"spring-web-4.1.2.RELEASE.jar"
"spring-webmvc-4.1.2.RELEASE.jar"
"validation-api-1.0.0.GA.jar"
//rest web service
@Controller
public class Lo_Controller {
@RequestMapping(value="myUrl", method=RequestMethod.POST)
@ResponseBody
//client call
LogDisplay _l = restTemplate.postForObject("http://localhost:8080/myApp/myRestService",myObjWithPasrameters, LogDisplay.class);
//it will print class java.util.ArrayList
System.out.printLn((lo_Values.setDisplayValues((_l.getDisplayValues()).getClass());
//it will print class java.util.LinkedHashMap but I was expecting to get Lo_DisplayRecord type
System.out.printLn((lo_Values.setDisplayValues(_l.getDisplayValues().get(0))).getClass());
/* when trying to cast (Lo_DisplayRecord)displayValues.get(0)
I got the error
java.lang.ClassCastException: java.util.LinkedHashMap
cannot be cast to com.my_app. Lo_DisplayRecord
so I fixed it by using jackson.map.ObjectMapper as suggested in
https://stackoverflow.com/questions/15430715/casting-linkedhashmap-to-complex-object
*/
//Solution 1:
ObjectMapper mapper = new ObjectMapper();
Lo_DisplayRecord pojo = mapper.convertValue(displayValues.get(0), Lo_DisplayRecord.class);
//Solution 2:
byte[] json = mapper.writeValueAsBytes(displayValues.get(0));
Lo_DisplayRecord pojo2 = mapper.readValue(json, Lo_DisplayRecord.class);
//the pojo
public class LogDisplay {
private ArrayList<Lo_DisplayRecord> displayValues;
//... others variables not relevants to the question
public ArrayList<Lo_DisplayRecord> getDisplayValues() {
return displayValues;
}