12

I am using RestTemplate and having issues deserializing an object. Here is what I am doing. 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 {
    @JsonProperty("response")
    private Response response;
    @JsonProperty("message")
    private Object message;
    @JsonProperty("code")
    private Object code;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    //bunch of getters and setters here
}
public class Response {
    @JsonProperty("Time")
    private Date Time;
    @JsonProperty("Total_Input_Records")
    private Object Total_Input_Records;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//bunch of getters and setters here
}

Here is the request processing where I am getting the exception,

String url = "http://example.com/someparams";
RestTemplate template = new RestTemplate(
                new HttpComponentsClientHttpRequestFactory());
FormHttpMessageConverter converter = new FormHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(new MediaType("application", "x-www-form-urlencoded"));
converter.setSupportedMediaTypes(mediaTypes);
template.getMessageConverters().add(converter);
MyClass upload = template.postForObject(url, null, MyClass.class);

Here is the frustrating part, exception (deliberately trimmed, not full). Anything I am missing?

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "Time" (Class com.temp.pointtests.Response), not marked as ignorable
 at [Source: org.apache.http.conn.EofSensorInputStream@340ae1cf; line: 1, column: 22  (through reference chain: com.temp.pointtests.MyClass["response"]->com.temp.pointtests.Response["Time"]);] 

+++++Update Solved++++++

I saw Spring added MappingJackson2HttpMessageConverter which uses Jackson 2. Because MappingJacksonHttpMessageConverter in my code above uses Jackson Pre2.0 versions and it doesn't work. It works for Jackson 2.0 however. With MappingJackson2HttpMessageConverter available now, I can now add it to my RestTemplate and everything works fine :-). Here is the code for people who have the same issue,

String url = "http://example.com/someparams";
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  
HttpEntity request = new HttpEntity(headers);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter map = new MappingJackson2HttpMessageConverter();
messageConverters.add(map);
messageConverters.add(new FormHttpMessageConverter());
template.setMessageConverters(messageConverters);
MyClass msg = template.postForObject(url, request, MyClass.class);
2
  • 1
    you should answer your own question with the "Update edit" of the question. Commented Jun 18, 2013 at 14:02
  • 1
    You are awesome, your update resolved the issue. Commented Jul 28, 2020 at 17:02

1 Answer 1

0

Use the @JsonSerialize(using = JsonDateSerializer.class) or @JsonDeserialize(using = JsonDateDeSerializer.class) annotations of org.codehaus.jackson.map.JsonDeserializer; it will solve the issue or user ObjectMapper(org.codehaus.jackson.map.ObjectMapper) to convert to Json String.

objectMapper.writeValueAsString(Object); // this will give json string

Sign up to request clarification or add additional context in comments.

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.