gpt4 book ai didi

java - 使用 RestTemplate 反序列化嵌套对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:37:42 25 4
gpt4 key购买 nike

我正在使用 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);

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

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 并且一切正常:-)。这是给有同样问题的人的代码,

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

最佳答案

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

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

关于java - 使用 RestTemplate 反序列化嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14491309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com