Java爬坑 -- 解決redis序列化java8 LocalDateTime錯誤的問題

redis序列化選擇方式java

 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 Demoredis

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

在redis中查看spring

 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 ......

修改app

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裏面對象ui

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

而後就能夠解決問題了spa

相關文章
相關標籤/搜索