redis序列化选择方式
1 1 public CacheManager cacheManager(RedisTemplate redisTemplate) { 2 2 //设置序列化Key的实例化对象 3 3 redisTemplate.setKeySerializer(new StringRedisSerializer()); 4 4 //设置序列化Value的实例化对象 5 5 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); 6 6 RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate); 7 7 // 使用前缀 8 8 redisCacheManager.setUsePrefix(true); 9 9 redisCacheManager.setCachePrefix(new DefaultRedisCachePrefix(":")); 1010 // 设置默认缓存的过期时间 1111 redisCacheManager.setDefaultExpiration(86400);// 一天 1212 1313 redisCacheManager.setExpires(expires); 1414 1515 return redisCacheManager;
要序列化class Demo
11 public class Demo { 22 private Long id; 33 private String name; 44 private LocalDateTime time; 55 66 }
在redis中查看
1 1 { 2 2 "@class": "com.karmay3d.Demo", 3 3 "id": 10000000001, 4 4 "name": "测试序列化", 5 5 "time": { 6 6 "dayOfMonth": 15, 7 7 "dayOfWeek": "TUESDAY", 8 8 "dayOfYear": 227, 9 9 "month": "AUGUST", 1010 "monthValue": 8, 1111 "year": 2017, 1212 "hour": 14, 1313 "minute": 45, 1414 "second": 51, 1515 "nano": 921000000, 1616 "chronology": { 1717 "@class": "java.time.chrono.IsoChronology", 1818 "id": "ISO", 1919 "calendarType": "iso8601" 2020 } 2121 } 2222 }
报的异常
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?) 22 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?) 33 at [Source: [B@68d651f2; line: 1, column: 81] (through reference chain: com.karmay3d.Demo["time"]) 44 ...... 55 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?) 66 at [Source: [B@68d651f2; line: 1, column: 81] (through reference chain: com.karmay3d.Demo["time"]) 77 at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270) 88 at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456) 99 ......
修改
LocalDateTime属性加上注解
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
11 public class Demo { 22 private Long id; 33 private String name; 44 @JsonDeserialize(using = LocalDateTimeDeserializer.class) 55 @JsonSerialize(using = LocalDateTimeSerializer.class) 66 private LocalDateTime time; 77 ...... 88 }
redis里面对象
11 { 22 "@class": "com.karmay3d.Demo", 33 "id": 10000000001, 44 "name": "测试序列化", 55 "time": [2017,8,15,14,57,37,525000000] 66 }
然后就可以解决问题了