[Annotation use] Use @JsonFormat annotation to step on the pit

Contents of this article

1. Background description

Second, the cause of the error

Three, the solution


1. Background description

Project technology stack: Spring boot (2.1.6.RELEASE) + JDK (1.8) + Maven (apache-maven-3.5.4) + Mybatis-plus (3.4.0)

When working on a project system recently, the Java back-end is required to return the time to the front-end page (local time-Beijing time), back-end time field mapping, conversion, no matter how to deal with it, it is based on Beijing time, this is correct of. But as soon as it is displayed on the front end, it will be 8 hours less than Beijing time. After checking all possible errors, I feel that the code is written without problems. But the front-end display is still less than 8 hours, so there must still be a problem, look for it. My entity class is as follows: Please pay attention to the timezone = "GMT + 8" enclosed by the red frame. Will be used later.

Second, the cause of the error

This is also caused by a habit of mine. The timezone enclosed by the red frame = "GMT + 8" has a space on each side of the plus sign, and this time zone is not recognized during serialization . As a result, 8 hours were lost when it was passed to the front end for display. For details, see the figure below.

Three, the solution

After knowing the cause of the problem, it is easy to correct the problem, just delete the space in the middle. As shown below.

Test again and get it done.

 

Extension: Use of @JsonFormat and @DateTimeFormat annotations: https://blog.csdn.net/weixin_44299027/article/details/102931545

 

 

end!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/110677354