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;
推荐阅读
- Spring中ApplicationListener的使用
- Springboot+Mybatisplus替换mybatis整合报错Mapped Statements collection does not contain value
- springmvc-@RequestMapping
- spring设计模式之applicationContext.getBean("beanName")思想
- 「Spring源码」循环依赖解析&遇到的bug分析
- 推荐!Spring MVC面试题和答案整理
- 精品!Spring面试题和答案合集详解
- 史上最全!Spring Boot面试问题和答案整理
- Spring按业务模块输出日志到不同的文件
- spring 的 ApplicationContext.getBean(type)无法获取bean,报错