Solve the serialization problem of using java 8 time and date APIs (localdate, etc.) in spring boot and feign
Localdate, Localtime and localdatetime are the time and date APIs provided by Java 8. They are mainly used to optimize the processing of time and date in Java 8. However, when using spring boot or spring cloud feign, we often find that various problems will occur when using request parameters or returning results with localdate, Localtime and localdatetime. In this paper, we will talk about the problems in this case and how to solve them.
Problem phenomenon
Let's see the symptoms first. For example, the following example:
The above code constructs a simple spring boot web application, which provides an interface to submit user information, which contains data of localdate type. At this time, if we use feign to call this interface, we will get the following error:
Analysis and solution
For the above error message JSON parse error: can not construct instance of Java time. Localdate: no suitable constructor found, can not deserialize from object value. Children's shoes familiar with spring MVC should be able to locate immediately. The error is related to the deserialization of localdate. However, there will still be many readers will be this error message Java util. ArrayList[0]->com. didispace. Userdto ["birthday"]. We named the submitted userdto ["birthday"] a localdate object. What does it have to do with the ArrayList list object?
We might as well send a request manually through postman to see what is returned by the server? For example, you can initiate a request according to the following figure:
From the above figure, we can understand the confusion I mentioned above. In fact, by default, spring MVC serializes localdate into an array type, and feign still processes it according to ArrayList when calling, so naturally it cannot be deserialized into localdate object.
resolvent
In order to solve the above problem, it is very simple, because Jackson also provides a complete set of serialization scheme. We only need to Jackson-datatype-jsr310 dependency is introduced into XML, as follows:
Note: when the parent of spring boot is set, it is not necessary to specify a specific version, and it is not recommended to specify a specific version
The module encapsulates the implementation of time date API serialization of Java 8. Its specific implementation is in this class: com fasterxml. jackson. datatype. jsr310. Javatimemodule (Note: some earlier versions are wildly changed to "com.fasterxml.jackson.datatype.jsr310.jsr310module" in this class). After configuring the dependency, we only need to add this serialization module in the above application main class and open the standard ISO 8601 format:
At this time, when we access the interface just now, it is no longer an array type. At the same time, the above error will not appear in the call to feign client.
Code example
For relevant examples in this article, you can view the chapter 3-1-7 directory in the following warehouse:
Github: https://github.com/dyc87112/SpringBoot-Learning
Gitee: https://gitee.com/didispace/SpringBoot-Learning
summary
The above is what Xiaobian introduced to you to solve the serialization problem of using java 8 time and date API (localdate, etc.) in spring boot and feign. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you for your support for the programming tips website!