Spring Serializing and Deserializing Boot Field LocalDate

In spring, loading 1.2.3.RELEASE with quickxml, what is the correct way to serialize and de-serialize the LocalDate field for an iso date formatted string?

I tried:

spring.jackson.serialization.write-date-as-timestamps: false in application.properties file,

including jackson-datatype-jsr310 in the project and then using

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

annotation

and @DateTimeFormat(iso=ISO.DATE)

annotation,

adding Jsr310DateTimeFormatAnnotationFormatterFactory as formatting with:

@Override public void addFormatters (FormatterRegistry) {registry.addFormatterForFieldAnnotation (new Jsr310DateTimeFormatAnnotationFormatterFactory ()); }

None of the above helped.

+3


source to share


3 answers


compile ("com.fasterxml.jackson.datatype: jackson-datatype-jsr310") in build.gradle and then the following annotations helped:



@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate birthday;

      

+13


source


Actually, it works if you just specify the dependency in the pom.xml.

In this case, all LocalDate fields automatically use the ISO format, you do not need to comment on them:



<!-- This is enough for LocalDate to be deserialized using ISO format -->
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

      

Tested on Spring Boot 1.5.7.

+3


source


If you want to use the native Java date formatter add the annotation @JsonFormat

.

@JsonFormat(pattern = "dd/MM/yyyy")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate birthdate;*

      

+1


source







All Articles