SpringBoot - 优雅解决 SpringBoot 在 JDK8 中 LocalDateTime(反)序列化问题

简介: SpringBoot - 优雅解决 SpringBoot 在 JDK8 中 LocalDateTime(反)序列化问题

在做项目的时候很容易遇到这种问题:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.time.LocalDateTime from String \"2020-02-15 22:13:15\": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-15 22:13:15' could not be parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.LocalDateTime from String \"2020-02-15 22:13:15\": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-15 22:13:15' could not be parsed at index 10\n at [Source: (PushbackInputStream); line: 1, column: 15]

出现这种问题的原因是项目中使用了 JDK8 中全新的日期和时间类:LocalDateTime。LocalDateTime 默认的时间格式是: yyyy-MM-ddTHH:mm:ss,中间多了一个 T 字,但是对于往常的数据来说是没有 T 字的,这就会造成后端在接受或返回时间数据的时候出现异常,解决方案如下(添加一个配置类,千万不要忘了 @Configuration 注解):


import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
 * @author lux sun
 */
@Configuration
public class LocalDateTimeConfiguration {
    @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
    private String pattern;
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            //返回时间数据序列化
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
            //接收时间数据反序列化
            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
        };
    }
}
  • 【注】服务器接收前端的时间数据并处理是反序列化:将流数据转换成 LocalDateTime,服务器返回给前端数据是序列化:将 LocalDateTime 数据转换成流;两个一般成对出现,也可以只用其中的某一个。


目录
相关文章
|
2月前
|
Java
LocalDateTime的全局自定义序列化
LocalDateTime的全局自定义序列化
|
4月前
|
设计模式 JSON 前端开发
SpringBoot中对LocalDateTime进行格式化并解析
SpringBoot中对LocalDateTime进行格式化并解析
155 0
|
2月前
|
设计模式 安全 Java
深入理解Spring Boot AOP:CGLIB代理与JDK动态代理的完全指南
深入理解Spring Boot AOP:CGLIB代理与JDK动态代理的完全指南
340 1
|
4月前
|
存储 缓存 NoSQL
SpringBoot整合Redis调用lua脚本出现空指针异常(序列化器问题)
一、问题描述 业务中出现需要保证原子性的一系列缓存操作,所以决定使用lua脚本来保证原子性。 但是调用过程中lua脚本抛出了异常:attempt to perform arithmetic on local ‘xxx’ (a nil value) 发生异常的lua脚本代码(部分)
|
7月前
|
缓存 NoSQL Java
SpringBoot自定义redisTemplate的key和value的序列化方式
SpringBoot自定义redisTemplate的key和value的序列化方式
103 0
|
8月前
|
存储 缓存 NoSQL
SpringBoot整合Redis调用lua脚本出现空指针异常(序列化器问题)
一、问题描述 业务中出现需要保证原子性的一系列缓存操作,所以决定使用lua脚本来保证原子性。 但是调用过程中lua脚本抛出了异常:attempt to perform arithmetic on local ‘xxx’ (a nil value) 发生异常的lua脚本代码(部分)
|
9月前
|
SQL Java 关系型数据库
从0开始,搭建springboot后台工程搭建及解释(从jdk 及 maven 讲起)(2)
从0开始,搭建springboot后台工程搭建及解释(从jdk 及 maven 讲起)
116 0
|
9月前
|
数据可视化 Java 项目管理
从0开始,搭建springboot后台工程搭建及解释(从jdk 及 maven 讲起)(1)
从0开始,搭建springboot后台工程搭建及解释(从jdk 及 maven 讲起)
102 0
|
10月前
|
JSON 前端开发 Java
【工作中问题解决实践 七】SpringBoot集成Jackson进行对象序列化和反序列化
【工作中问题解决实践 七】SpringBoot集成Jackson进行对象序列化和反序列化
262 0
|
10月前
|
Java
LocalDateTime序列化成yyyy-MM-dd HH:mm
LocalDateTime序列化成yyyy-MM-dd HH:mm
139 0