We often have scenes of front-end and back-end time conversion in projects, such as: creating time, update time, etc. Generally, the front and back ends use the timestamp or year, month and day format for transmission.
If the backend receives the front-end parameters and manually converts them into the desired format every time, it would be too troublesome for the backend to manually process the data into the desired format every time it passes the data to the front-end.
It needs to be configured in two scenarios (according to different Content-Type):
1.application/x-www-form-urlencoded and multipart/ form-data
This situation is recorded here as: not using @RequestBody
2.application/json
That is: using the interface of @RequestBody
This situation is recorded as: using @RequestBody
Remarks
Some people say that it can be configured like this:
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT 8
serialization:
write-dates-as-timestamps: false
This configuration is only applicable to Date, not LocalDateTime, etc.
Date serialization/deserialization uses this format: "2020-08-19T16:30:18.823 00:00".
Configuration class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Test
postman visit: http://localhost:8080/user/save? userName=Tony&createTime=2021-09-16 21:13:21
postman result:
Configuration Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Test
postman access: http://localhost:8080/user/save?userName=Tony&createTime=2021-09-16 21:13:21postman results:Configuration Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
Entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Test
Save user: User(id=null, userName=Tony, createTime=2021- 09-16T21:13:21)Method 2: Configure class @JsonFormatThis method needs to configure ObjectMapper, and Entity also needs to add @JsonFormat.
Configuration Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Entity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Test
Save user: User(id=null, userName=Tony, createTime=2021- 09-16T21:13:21)Option 2: Jackson2ObjectMapperBuilderCustomizer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
The above is the detailed content of What is the method for SpringBoot LocalDateTime format conversion?. For more information, please follow other related articles on the PHP Chinese website!