Home > Java > javaTutorial > body text

Solving the problem of SpringBoot returning JSON date format

不言
Release: 2018-09-20 14:36:38
Original
3585 people have browsed it

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
Copy after login

or this:

"createTime": "2018-09-18T10:54:06.000+0000"
Copy after login

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
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template