Announcement Announcement Module
No announcement yet.
Problem with Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with

    This ties to my other question here, in which I had issues with deserializing object using RestTemplate. I thought of deserializing the object myself using which RestTemplate internally uses. I am still not able deserialize using However interestingly, I am able to deserialize it successfully using com.fasterxml.jackson.databind.ObjectMapper. This explains why RestTemplate is failing to deserialize?

    The JSON response looks like,

    "response": {
    "Time": "Wed 2013.01.23 at 03:35:25 PM UTC",
    "Total_Input_Records": 5,
    "message": "Succeeded",
    "code": "200"
    Converted this Json payload into a POJO using jsonschema2pojo

    public class MyClass {
        private Response response;
        private Object message;
        private Object code;
        private Map<String, Object> additionalProperties = new HashMap<String, Object>();
        //bunch of getters and setters here
    public class Response {
        private Date Time;
        private Object Total_Input_Records;
        private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    //bunch of getters and setters here
    Here is the deserialization code,
    String jsonStr = "{\"response\": {"
    				+ "\"Time\": \"Wed 2013.01.23 at 03:35:25 PM UTC\","
    				+ "\"Total_Input_Records\": \"5\"},"
    				+ "\"message\": \"Succeeded\"," + "\"code\": \"200\"" + "}";
    ObjectMapper mapper = new ObjectMapper();
    MyClass wrapper mapper.readValue(jsonStr, MyClass.class);

    Below is the exception I get with[/CODE]. Again, this exact code WORKS with com.fasterxml.jackson.databind.ObjectMapper
    Code: Unrecognized field "Time" (Class com.temp.pointtests.Response), not marked as ignorable
     at [Source: [email protected]; line: 1, column: 24] (through reference chain: MyClass["response"]->Response["Time"])

  • #2
    I think I know what the issue is. I saw Spring added MappingJackson2HttpMessageConverter which uses Jackson 2. I can now add MappingJackson2HttpMessageConverter to my RestTemplate and everything works fine :-). Here is the code for people who have the same issue,

    String url = "";
    RestTemplate template = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    HttpEntity request = new HttpEntity(headers);
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    MappingJackson2HttpMessageConverter map = new MappingJackson2HttpMessageConverter();
    messageConverters.add(new FormHttpMessageConverter());
    MyClass msg = template.postForObject(url, request, MyClass.class);