json date format in spring-boot

There are three things that you need to do to format the date as yyyy-MM-dd:

  1. 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.
  2. Configure Jackson not to format dates as timestamps by adding spring.jackson.serialization.write-dates-as-timestamps: false to your application.properties file.
  3. 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:

  1. To take care send date from client as string object, in format yyyy/MM/dd

  2. 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