使用 RestTemplate 反序列化嵌套对象

我正在使用 RestTemplate 并且在反序列化对象时遇到问题。这就是我正在做的事情。 JSON 响应看起来像,

{
    "response": {
        "Time": "Wed 2013.01.23 at 03:35:25 PM UTC",
        "Total_Input_Records": 5,
    },-
    "message": "Succeeded",
    "code": "200"
}

使用 jsonschema2pojo 将此 Json 有效负载转换为 POJO

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
}

这是我遇到异常的请求处理,

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);

这是令人沮丧的部分,异常(故意修剪,不完整)。有什么我想念的吗?

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"]);] 

+++++更新已解决++++++

我看到 Spring 添加了使用 Jackson 2 的 MappingJackson2HttpMessageConverter。因为我上面的代码中的 MappingJacksonHttpMessageConverter 使用 Jackson Pre2.0 版本并且它不起作用。但是它适用于杰克逊 2.0。随着 MappingJackson2HttpMessageConverter 现在可用,我现在可以将它添加到我的 RestTemplate 并且一切正常:-)。这是有相同问题的人的代码,

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);
stack overflow Deserializing Nested objects using RestTemplate
原文答案

答案:

作者头像

使用 org.codehaus.jackson.map.JsonDeserializer 的 @JsonSerialize(using = JsonDateSerializer.class) 或 @JsonDeserialize(using = JsonDateDeSerializer.class) 注解;它将解决问题或用户 ObjectMapper(org.codehaus.jackson.map.ObjectMapper) 转换为 Json 字符串。

objectMapper.writeValueAsString(Object); // 这将给出 json 字符串