Spring|Spring Cloud通过Feign请求返回值LocalDateTime异常处理

问题描述 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,所以相同的转换器在不同的调用方式下产生了不一样的结果仍然不得而知。
问题解决 增加依赖
com.fasterxml.jackson.datatype jackson-datatype-jsr310 2.9.9

字段增加注解
【Spring|Spring Cloud通过Feign请求返回值LocalDateTime异常处理】POJO类的LocalDateTime类型字段增加如下注解
@JsonDeserialize(using = LocalDateTimeDeserializer.class) private LocalDateTime createTime;

    推荐阅读