实体类中包含了LocalDateTime 类型的属性,把实体类数据存入Redis后变成这样:
此时,存入redis不会报错,但是从redis获取的时候,会报错:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
大概意思是反序列化反不回来了。
那么可以在LocalDateTime类型的属性上加注解指定序列化/反序列化器:
@ApiModelProperty(value = "创建时间") @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime time;
注意引入的包:
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
再次启动,向缓存里放数据,LocalDateTime的数据显示正常,不会多出很多数据:
获取缓存也不会报错了!