Spring Boot LocalDate field serialization and deserialization

浪子不回头ぞ 提交于 2019-11-28 01:35:29

问题


On spring boot 1.2.3.RELEASE with fasterxml what is the correct way of serializing and de-serializing a LocalDate field to iso date formatted string?

I've tried:

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

including jackson-datatype-jsr310 in project and then using

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

and @DateTimeFormat(iso=ISO.DATE) annotation,

adding Jsr310DateTimeFormatAnnotationFormatterFactory as formatter with:

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

None of the above helped.


回答1:


compile ("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

in build.gradle and then following annotations helped:

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

Update: if you are using Spring Boot 2.*, the dependency is already included via one of the "starters".




回答2:


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

With this, all my LocalDate fields automatically use the ISO format, no need to annotate 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:


If you want to use a custom Java Date formatter, add the @JsonFormat annotation.

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



回答4:


In my Spring Boot 2 applications

  • @JsonFormat annotation is used in REST controllers when (de)serializing JSON data.
  • @DateTimeFormat annotation is used in other controllers ModelAttributes when (de)serializing String data.

You can specify both on the same field (useful if you share DTO between JSON and Thymeleaf templates):

@JsonFormat(pattern = "dd/MM/yyyy") 
@DateTimeFormat(pattern = "dd/MM/yyyy")
private LocalDate birthdate;

Gradle dependency:

implementation group: 'org.springframework.boot', name: 'spring-boot-starter-json'

I hope this is all configuration you need for custom Date/Time formatting in Spring Boot 2.x apps.


For Spring Boot 1.x apps, specify additional annotations and dependency:

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

// or
@JsonFormat(pattern = "dd/MM/yyyy HH:mm")
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime birthDateTime;
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.8'


来源:https://stackoverflow.com/questions/30871255/spring-boot-localdate-field-serialization-and-deserialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!