The content of this article is about solving the problem of SpringBoot returning JSON date format. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The default date format returned in SpringBoot is similar to this:
"birth": 1537407384500
or this:
"createTime": "2018-09-18T10:54:06.000+0000"
None of the above meet the actual display needs
Modification method (limited to the case of using the default jackson parsing package):
Modify the default in the application.properties/yml file format format:
spring.jackson.date-format=yyyy-MM-dd spring.jackson.time-zone=GMT+8 spring.jackson.serialization.write-dates-as-timestamps=false
The value of spring.jackson.date-format above can be modified according to actual needs.
Then a problem will arise after the modification: What if I want to return a different format? For example, yyyy-MM-dd or yyyy year MM month dd day HH hour mm minute ss second
Then you can set a default format in the above configuration file, and then if you need other formats, you only need to add the relevant Add the following annotation to the field in the entity class:
@JsonFormat(pattern="yyyy年MM月dd日 HH时mm分ss秒",timezone = "GMT+8") private Date registerDate;
At this time, the return format will give priority to the format set by the annotation. Flexibility can be achieved through the above methods.
The above is the detailed content of Solving the problem of SpringBoot returning JSON date format. For more information, please follow other related articles on the PHP Chinese website!