解决JDK8中LocalDateTime(反)序列化问题,报错com.fasterxml.jackson.datatype:jackson-datatype-jsr310

参考文章:

  1. https://www.shouxicto.com/article/2858.html
  2. https://blog.csdn.net/weixin_43840323/article/details/120077245

问题复现:

Java 8 date/time type `java.time.LocalDateTime` not supported by default:
 add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling....

默认情况下,SpringBoot 搭配 Java 8不支持LocalDateTime需要添加com.fasterxml.jackson.datatype:jackson-datatype-jsr310依赖

原因:没有添加序列化和反序列化器

解决方案
添加依赖

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.13.0</version>
</dependency>

为所有LocalDateTime字段添加序列化以及反序列化器注解,例如:

 @JsonSerialize(using = LocalTimeSerializer.class)
 @JsonDeserialize(using = LocalDateTimeDeserializer.class)
 @JsonFormat(pattern = "HH:mm:ss")
 private LocalTime xxx;
 
 @JsonSerialize(using = LocalDateTimeSerializer.class)
 @JsonDeserialize(using = LocalDateTimeDeserializer.class)
 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 private LocalDateTime xxx;
 
 @JsonSerialize(using = LocalDateSerializer.class)
 @JsonDeserialize(using = LocalDateTimeDeserializer.class)
 @JsonFormat(pattern = "yyyy-MM-dd")
 private LocalDate xxx;

你可能感兴趣的:(java,java,spring,boot,spring)