Spring Boot和Feign中使用Java 8時(shí)間日期API(LocalDate等)的序列化問(wèn)題
LocalDate、LocalTime、LocalDateTime是Java 8開(kāi)始提供的時(shí)間日期API,主要用來(lái)優(yōu)化Java 8以前對(duì)于時(shí)間日期的處理操作。然而,我們?cè)谑褂肧pring Boot或使用Spring Cloud Feign的時(shí)候,往往會(huì)發(fā)現(xiàn)使用請(qǐng)求參數(shù)或返回結(jié)果中有LocalDate、LocalTime、LocalDateTime的時(shí)候會(huì)發(fā)生各種問(wèn)題。本文我們就來(lái)說(shuō)說(shuō)這種情況下出現(xiàn)的問(wèn)題,以及如何解決。
問(wèn)題現(xiàn)象
先來(lái)看看癥狀。比如下面的例子:
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- @RestController
- class HelloController {
- @PostMapping("/user")
- public UserDto user(@RequestBody UserDto userDto) throws Exception {
- return userDto;
- }
- }
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- static class UserDto {
- private String userName;
- private LocalDate birthday;
- }
- }
上面的代碼構(gòu)建了一個(gè)簡(jiǎn)單的Spring Boot Web應(yīng)用,它提供了一個(gè)提交用戶信息的接口,用戶信息中包含了LocalDate類型的數(shù)據(jù)。此時(shí),如果我們使用Feign來(lái)調(diào)用這個(gè)接口的時(shí)候,會(huì)得到如下錯(cuò)誤:
- 2018-03-13 09:22:58,445 WARN [http-nio-9988-exec-3] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
- at [Source: java.io.PushbackInputStream@67064c65; line: 1, column: 63] (through reference chain: java.util.ArrayList[0]->com.didispace.UserDto["birthday"])
分析解決
對(duì)于上面的錯(cuò)誤信息JSON parse error: Can not construct instance of java.time.LocalDate: no suitable constructor found, can not deserialize from Object value,熟悉Spring MVC的童鞋應(yīng)該馬上就能定位錯(cuò)誤與LocalDate的反序列化有關(guān)。但是,依然會(huì)有很多讀者會(huì)被這段錯(cuò)誤信息java.util.ArrayList[0]->com.didispace.UserDto["birthday"]所困惑。我們命名提交的UserDto["birthday"]是個(gè)LocalDate對(duì)象嘛,跟ArrayList列表對(duì)象有啥關(guān)系呢?
我們不妨通過(guò)postman等手工發(fā)一個(gè)請(qǐng)求看看服務(wù)端返回的是什么?比如你可以按下圖發(fā)起一個(gè)請(qǐng)求:
從上圖中我們就可以理解上面我所提到的困惑了,實(shí)際上默認(rèn)情況下Spring MVC對(duì)于LocalDate序列化成了一個(gè)數(shù)組類型,而Feign在調(diào)用的時(shí)候,還是按照ArrayList來(lái)處理,所以自然無(wú)法反序列化為L(zhǎng)ocalDate對(duì)象了。
解決方法
為了解決上面的問(wèn)題非常簡(jiǎn)單,因?yàn)閖ackson也為此提供了一整套的序列化方案,我們只需要在pom.xml中引入jackson-datatype-jsr310依賴,具體如下:
- <dependency>
- <groupId>com.fasterxml.jackson.datatype</groupId>
- <artifactId>jackson-datatype-jsr310</artifactId>
- </dependency>
注意:在設(shè)置了spring boot的parent的情況下不需要指定具體的版本,也不建議指定某個(gè)具體版本
在該模塊中封裝對(duì)Java 8的時(shí)間日期API序列化的實(shí)現(xiàn),其具體實(shí)現(xiàn)在這個(gè)類中:com.fasterxml.jackson.datatype.jsr310.JavaTimeModule(注意:一些較早版本瘋轉(zhuǎn)在這個(gè)類中“com.fasterxml.jackson.datatype.jsr310.JSR310Module)。在配置了依賴之后,我們只需要在上面的應(yīng)用主類中增加這個(gè)序列化模塊,并禁用對(duì)日期以時(shí)間戳方式輸出的特性:
- @Bean
- public ObjectMapper serializingObjectMapper() {
- ObjectMapper objectMapper = new ObjectMapper();
- objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
- objectMapper.registerModule(new JavaTimeModule());
- return objectMapper;
- }
此時(shí),我們?cè)谠L問(wèn)剛才的接口,就不再是數(shù)組類型了,同時(shí)對(duì)于Feign客戶端的調(diào)用也不會(huì)再出現(xiàn)上面的錯(cuò)誤了。
代碼示例
本文的相關(guān)例子可以查看下面?zhèn)}庫(kù)中的Chapter3-1-7目錄:
Github:https://github.com/dyc87112/SpringBoot-Learning
Gitee:https://gitee.com/didispace/SpringBoot-Learning
【本文為51CTO專欄作者“翟永超”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)通過(guò)51CTO聯(lián)系作者獲取授權(quán)】