使用 Jackson 作为 HttpMessageConverter 和 joda DateTime 属性的 Spring RestTemplate 无法反序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21355450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring RestTemplate with Hymanson as HttpMessageConverter and joda DateTime property fails to deserialize
提问by Spase Markovski
The scenario is as follows. I have an ObjectMapper (Hymanson 2) that registers a JodaModule, capable of serializing and de-serializing Joda DateTime type. This ObjectMapper is tested with custom JSON strings and works as expected.
场景如下。我有一个注册 JodaModule 的 ObjectMapper(Hymanson 2),能够序列化和反序列化 Joda DateTime 类型。此 ObjectMapper 已使用自定义 JSON 字符串进行测试并按预期工作。
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
objectMapper.setDateFormat(new ISO8601DateFormat());
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper;
I have an RestTemplateFactory which is responsible for instantiating a RestTemplate, and it sets the previously configured ObjectMapper bean to the RestTemplate.
我有一个 RestTemplateFactory 负责实例化一个 RestTemplate,它将之前配置的 ObjectMapper bean 设置为 RestTemplate。
@Configuration
public class RestTemplateFactory {
@Autowired
private ObjectMapper objectMapper;
@Bean
public RestTemplate createRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
MappingHymanson2HttpMessageConverter jsonMessageConverter = new MappingHymanson2HttpMessageConverter();
jsonMessageConverter.setObjectMapper(objectMapper);
messageConverters.add(jsonMessageConverter);
// restTemplate.setMessageConverters(messageConverters); // This line was missing, but needs to be here. See answer.
return restTemplate;
}
}
Now when I contact the webservice it fails to de-serialize the DateTime object with the following error message:
现在,当我联系 Web 服务时,它无法反序列化 DateTime 对象,并显示以下错误消息:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not instantiate value of type [simple type, class org.joda.time.DateTime] from String value; no single-String constructor/factory method
Also the DateTimeDeserializer.class is never called. Anyone has an idea what I am missing here?
也永远不会调用 DateTimeDeserializer.class。任何人都知道我在这里缺少什么?
回答by Spase Markovski
OK, I was missing this line in my createRestTemplate() method.
好的,我在 createRestTemplate() 方法中遗漏了这一行。
restTemplate.setMessageConverters(messageConverters);
回答by Rohit Naik
Add dependency
添加依赖
<dependency>
<groupId>com.fasterxml.Hymanson.datatype</groupId>
<artifactId>Hymanson-datatype-joda</artifactId>
<version>2.9.0.pr4</version>
</dependency>
and use DateTimeDeserializer.class for deserializing as below
并使用 DateTimeDeserializer.class 进行反序列化,如下所示
@JsonDeserialize(using = DateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd.MM.yyyy", timezone = "Europe/Berlin")
private DateTime date;
works fine for me. No need to add a custom message convertor.
对我来说很好用。无需添加自定义消息转换器。

