Java爬坑

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 }

然后就可以解决问题了

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

解决Redis序列化Java8的LocalDateTime问题

在从Redis获取带有LocalDateTime类型属性的对象时,产生序列化和反序列化问题解决办法方式一:实体类上指定LocalDateTime的序列化器和反序列化器@JsonDeserialize(usingLocalDateTimeDeserializer.class)//反序列化@JsonSerialize(usingLocalDa

java中的序列化

一、什么是java序列化  序列化:将对象写入IO流反序列化:从IO流中恢复对象序列化机制允许将实现序列化的java对象转换为字节序列,这些字节序列可以保存在磁盘上也可以通过网络传输,字节序列也可以再恢复为原来的对象。序列化机制可以让对象不依附于程序独立存在。二、应用场景

java Redis Jedis存储Java对象

Redis入门–Jedis存储Java对象(Java序列化为byte数组方式)Redis入门–Jedis存储Java对象\(Java序列化为byte数组方式)原文地址:http://alanland.iteye.com/admin/blogs/1600685(https://www.oschina.net/action/GoT

java中的序列化方式及dubbo使用kryo序列化

java中的序列化方式:1\.自带序列化 ObjectInputSteam、ObjectOutStream等2\.hession23\.json,xml等格式4.kryo5.FST\dubbo直接多种序列化方式,默认是hession2.比较成熟,但是效率略低。可以配置使用kryo  <dubbo:

FastJson、Jackson、Gson进行Java对象转换Json的细节处理

Java对象转换Json的细节处理前言Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?对比以下json转换方式一、fastJson1、fastJson在转换java对象为json的时候,默认是不序列化nu