I am using spring-boot and I have an entity class defined something like this
import org.joda.time.LocalDateTime;
@Entity
public class Project {
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime start_date;
...
...
}
When this class is converted to JSON, the field gets converted to the following string representation
{"start_date":[2014,11,15,0,0,0,0],...., ...}
I want to have the json response as yyyy-MM-dd
.
I tried the @DateTimeFormat(iso = ISO.DATE)
annotation and that did not help either.
Is there an easy way to do this conversion to proper json format ?
There are three things that you need to do to format the date as yyyy-MM-dd
:
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.spring.jackson.serialization.write-dates-as-timestamps: false
to your application.properties
file.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
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
You can use @JsonFormat annotation in and the desired pattern like this without using any dependency :
@JsonFormat(pattern="yyyy-MM-dd")
private Date created_At;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.