redis序列化java8 LocalDateTime
阿新 • • 發佈:2018-11-11
org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Can not construct instance of
java.time.LocalDateTime: no suitable constructor found, can not deserialize from Object value (missing default
constructor or creator, or perhaps need to add/enable type information?) at [Source: [ [email protected]; line: 1, column: 84]
(through reference chain:code.ValidateCode["expireTime"]); nested exception
is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDateTime:
no suitable constructor found, can not deserialize from Object value (missing default constructor or
creator, or perhaps need to add/enable type information?)at [Source: [ [email protected]; line: 1, column: 84] (through reference
在redis序列化的時候使用LocalDateTime是會出現上述錯誤,解決方法:
LocalDateTime屬性加上註解
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; public class Demo { @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) private LocalDateTime expireTime; }
這樣就可以解決問題了。