Solve the date conversion exception JSON parse error: Cannot deserialize value of type `java.util.Date` from StringSummary

If you don't accumulate steps, you won't reach thousands of miles; if you don't accumulate small streams, you won't be able to form rivers and seas ----- to the struggling self

Scenes:

The front-end transmits date parameters to the back-end, and the back-end receives problems. I will summarize this low-level problem once I encounter it.

Documentation reference:

​​​​​​​​Spring Framework Chinese Documentation - Spring Framework 4.3.21.RELEASE Reference | Docs4dev Spring is an open source design-level framework, which solves the problem of loose coupling between the business logic layer and other layers, so It runs through the entire system application with interface-oriented programming ideas. Spring is a lightweight Java development framework that emerged in 2003 and was created by Rod Johnson. Simply put, Spring is a layered JavaSE/EE full-stack (one-stop) lightweight open source framework. https://www.docs4dev.com/docs/zh/spring-framework/4.3.21.RELEASE/reference/

Spring Chinese Official Documentation - hviger - Blog Garden Spring Boot Chinese Documentation https://www.docs4dev.com/docs/zh/spring-boot/1.5.9.RELEASE/reference Spring Framewo https://www.cnblogs.com/ zhaojinhui/p/16963459.html

Front-end date format: yyyy-MM-dd HH:mm:ss

 ​​​​​​

Correct way:

Method 1: The backend uses the date type to receive

 Method 2: The backend date type field only uses @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") to receive 

wrong way:

Method 1: Only use @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") to receive the backend date type field, and report an error directly when requesting.

Use the @DateTimeFormat annotation correctly

Method 1: Add @RequestBody to the receiving object and use it with @JsonFormat

 

Method 2: The receiving object does not add @RequestBody and the Date type field only uses @DateTimeFormate

 

 Note: The request body used here is form-date . If you use the request body json, you will not receive data if you use the following request backend date type field

Summarize:

When the front-end date string is passed to the back- end when the back-end receives it with a date type field

1. The receiving object uses @RequestBody with @DateTimeFormat and @JsonForma

2. The receiving object uses @RequestBody with @JsonForma to convert the date string into a date format

3. The receiving object does not need to be decorated with @RequestBody, the form-data type of the request body (json type is not allowed) can pass @DateTimeFormat

Convert date string to date format

Guess you like

Origin blog.csdn.net/qq_38423256/article/details/128938440