使用RestTemplate反序列化嵌套对象 [英] Deserializing Nested objects using RestTemplate

查看:2621
本文介绍了使用RestTemplate反序列化嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RestTemplate并且在反序列化对象时遇到问题。这就是我在做的事情。 JSON响应如下:

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"
}

使用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://my.site.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"]);] 

+++++更新已解决+++++++此外

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

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://mysite.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);


推荐答案

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

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); //这将给json字符串

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

这篇关于使用RestTemplate反序列化嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆