Java爬坑 -- 解决redis序列化java8 LocalDateTime错误的问题

redis序列化选择方式

 1  public CacheManager cacheManager(RedisTemplate redisTemplate) {
 2         //设置序列化Key的实例化对象
 3         redisTemplate.setKeySerializer(new StringRedisSerializer());
 4         //设置序列化Value的实例化对象
 5         redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
 6         RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
 7         // 使用前缀
 8         redisCacheManager.setUsePrefix(true);
 9         redisCacheManager.setCachePrefix(new DefaultRedisCachePrefix(":"));
10         // 设置默认缓存的过期时间
11         redisCacheManager.setDefaultExpiration(86400);// 一天
12     
13         redisCacheManager.setExpires(expires);
14 
15         return redisCacheManager;

要序列化class Demo

1 public class Demo {
2     private Long id;
3     private String name;
4     private LocalDateTime time;
5    
6 }

在redis中查看

 1 {
 2   "@class": "com.karmay3d.Demo",
 3   "id": 10000000001,
 4   "name": "测试序列化",
 5   "time": {
 6     "dayOfMonth": 15,
 7     "dayOfWeek": "TUESDAY",
 8     "dayOfYear": 227,
 9     "month": "AUGUST",
10     "monthValue": 8,
11     "year": 2017,
12     "hour": 14,
13     "minute": 45,
14     "second": 51,
15     "nano": 921000000,
16     "chronology": {
17       "@class": "java.time.chrono.IsoChronology",
18       "id": "ISO",
19       "calendarType": "iso8601"
20     }
21   }
22 }

报的异常

1 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?)
2  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?)
3  at [Source: [B@68d651f2; line: 1, column: 81] (through reference chain: com.karmay3d.Demo["time"])
4 ......
5 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?)
6  at [Source: [B@68d651f2; line: 1, column: 81] (through reference chain: com.karmay3d.Demo["time"])
7     at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
8     at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
9 ......

修改

LocalDateTime属性加上注解 
@JsonDeserialize(using = LocalDateTimeDeserializer.class) 
@JsonSerialize(using = LocalDateTimeSerializer.class)

1 public class Demo {
2     private Long id;
3     private String name;
4     @JsonDeserialize(using = LocalDateTimeDeserializer.class)
5     @JsonSerialize(using = LocalDateTimeSerializer.class)
6     private LocalDateTime time;
7     ......
8     }

redis里面对象

1 {
2   "@class": "com.karmay3d.Demo",
3   "id": 10000000001,
4   "name": "测试序列化",
5   "time": [2017,8,15,14,57,37,525000000]
6 }

然后就可以解决问题了

 

posted @ 2018-03-02 13:57  杨康是个大坏蛋  阅读(10515)  评论(1编辑  收藏  举报