问题描述
Spring Cloud通过Feign客户端调用HTTP接口,如果返回值中包含LocalDateTime
类型(包括其他JSR-310中java.time
包的时间类),在客户端可能会出现反序列化失败的错误。错误信息如下:
feign.codec.DecodeException: Type definition error: [simple type, class java.time.LocalDateTime]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-11-27T11:04:32')
Caused by: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.time.LocalDateTime]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-11-27T11:04:32')
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-11-27T11:04:32')
问题分析
搜索到一个类似问题:http://blog.didispace.com/Spring-Boot-And-Feign-Use-localdate/,但是文中提供的方法并不能解决问题。
经试验,Spring Cloud的HTTP接口直接调用的参数或返回值中的LocalDateTime
类型是可以正常序列化和反序列化的,但是通过Feign客户端调用,反序列化时就可能产生错误。
跟踪代码发现Feign客户端反序列化通过feign.codec.Decoder
接口的实现类SpringDecoder
最终调用org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
,这是默认的Spring MVC使用的HttpMessageConverter
,所以相同的转换器在不同的调用方式下产生了不一样的结果仍然不得而知。
问题解决
增加依赖
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.9</version>
</dependency>
字段增加注解
POJO类的LocalDateTime
类型字段增加如下注解
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime createTime;