json date format in spring-boot
There are three things that you need to do to format the date as yyyy-MM-dd
:
- Add a dependency on
com.fasterxml.jackson.datatype:jackson-datatype-joda
. Judging by the output you're getting at the moment, I think you may already have this dependency. - Configure Jackson not to format dates as timestamps by adding
spring.jackson.serialization.write-dates-as-timestamps: false
to yourapplication.properties
file. - Annotate the
LocalDataTime
field or getter method with@JsonFormat(pattern="yyyy-MM-dd")
Note: You'll need to use Spring Boot 1.2 for step 2 to work.
Without additional dependency - the only thing I had to do is:
To take care send date from client as string object, in format
yyyy/MM/dd
In Spring Boot application, to add annotation on the date field with the same format
public class Foo
{
@JsonFormat(pattern = "yyyy/MM/dd")
private Date dueDate;
}
Using Spring Boot 2.3.5 version
Update
Another option, instead of step 2, to modify application.properties file, add there the format for any Date object:
spring.jackson.date-format=yyyy/MM/dd