解决报错:org.springframework.data.redis.serializer.SerializationException: Could not write/read JSON
•
数据库
问题分析
在使用Redis缓存含有LocalDateTime类型变量的实体类时会产生序列化问题,这是由于在默认情况下Java 8不支持LocalDateTime类型导致的,因此需要添加序列化器和反序列化器。
报错信息
写入报错
org.springframework.data.redis.serializer.SerializationException: Could not write JSON: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module “com.fasterxml.jackson.datatype:jackson-datatype-jsr310” to enable handling
读取报错
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module “com.fasterxml.jackson.datatype:jackson-datatype-jsr310” to enable handling
解决方法
在实体类LocalDateTime类型变量上方添加@JsonSerialize和@JsonDeSerialize这两个注解即可
@JsonSerialize(using = LocalDateTimeSerializer.class)//序列化器 @JsonDeserialize(using = LocalDateTimeDeserializer.class)//反序列化器 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")//时间格式(含有日期和时间) private LocalDateTime createTime; @JsonSerialize(using = LocalDateSerializer.class)//序列化器 @JsonDeserialize(using = LocalDateDeserializer.class)//反序列化器 @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")//时间格式(仅有日期) private LocalDateTime createTime; @JsonSerialize(using = LocalTimeSerializer.class)//序列化器 @JsonDeserialize(using = LocalTimeDeserializer.class)//反序列化器 @JsonFormat(pattern = "HH:mm:ss", timezone = "GMT+8")//时间格式(仅有时间) private LocalDateTime createTime;
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/0fcda1198a.html
