数据设置时序列化,取出数据时反序列化,即Serialize和Deserialize
如果数据类型是LocalDateTime类型,则redis序列化设置进去的数据是:
"createTime": {
"date": {
"year": 2019,
"month": "MAY",
"day": 15,
"prolepticMonth": 24232,
"era": [
"java.time.chrono.IsoEra",
"CE"
],
"dayOfYear": 135,
"dayOfWeek": "WEDNESDAY",
"leapYear": false,
"dayOfMonth": 15,
"monthValue": 5,
"chronology": {
"id": "ISO",
"calendarType": "iso8601"
}
},
"time": {
"hour": 11,
"minute": 3,
"second": 43,
"nano": 758000000
},
"dayOfYear": 135,
"dayOfWeek": "WEDNESDAY",
"month": "MAY",
"dayOfMonth": 15,
"year": 2019,
"monthValue": 5,
"hour": 11,
"minute": 3,
"second": 43,
"nano": 758000000,
"chronology": [
"java.time.chrono.IsoChronology",
{
"id": "ISO",
"calendarType": "iso8601"
}
]
}
此时,程序中取出redis中的数据需要反序列化,这里我使用的反序列化器是下面这个:
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
Jackson2JsonRedisSerializer
由于无法处理LocalDateTime这样的数据,会报错:
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: [B@68d651f2; line: 1, column: 81] (through reference chain: com.karmay3d.Demo["time"]); 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: [B@68d651f2; line: 1, column: 81] (through reference chain: com.karmay3d.Demo["time"])
......
Caused by: 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: [B@68d651f2; line: 1, column: 81] (through reference chain: com.karmay3d.Demo["time"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
......
所以解决办法是,指定LocalDateTime这样的序列化以及反序列化器:
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
@ApiModelProperty(hidden = true)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
protected LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
@ApiModelProperty(hidden = true)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
protected LocalDateTime updateTime;
仅做记录。