Cerbis
Cerbis

Reputation: 515

define date serialization in spring boot on dto-level

Is there a way define the serialisation of dates in the DTO? The new default configuration from spring boot to serialise dates is now spring.jackson.serialization.write-dates-as-timestamps=false. I can change that, but it has a global effect on the serialisation.

My problem is that i have to support multiple api-versions. for example v1 needs the date to be serialized to a timestamp and v2 on the other hand as string.

Upvotes: 1

Views: 1103

Answers (1)

Karthik R
Karthik R

Reputation: 5786

If you use Jackson/FasterXml, you can use @JsonFormat. The annotation can be on both type level as well as per-field level.

Sample usage:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
private Date date;

Reference JavaDoc: https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonFormat.html

Upvotes: 1

Related Questions