Java爬坑

Wesley13
• 阅读 502

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 }

然后就可以解决问题了

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java中的序列化方式及dubbo使用kryo序列化
java中的序列化方式:1\.自带序列化 ObjectInputSteam、ObjectOutStream等2\.hession23\.json,xml等格式4.kryo5.FST\dubbo直接多种序列化方式,默认是hession2.比较成熟,但是效率略低。可以配置使用kryo  <dubbo:
好买-葡萄 好买-葡萄
2年前
解决Redis序列化Java8的LocalDateTime问题
在从Redis获取带有LocalDateTime类型属性的对象时,产生序列化和反序列化问题解决办法方式一:实体类上指定LocalDateTime的序列化器和反序列化器@JsonDeserialize(usingLocalDateTimeDeserializer.class)//反序列化@JsonSerialize(usingLocalDa
Wesley13 Wesley13
2年前
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
Wesley13 Wesley13
2年前
java中的序列化
一、什么是java序列化  序列化:将对象写入IO流反序列化:从IO流中恢复对象序列化机制允许将实现序列化的java对象转换为字节序列,这些字节序列可以保存在磁盘上也可以通过网络传输,字节序列也可以再恢复为原来的对象。序列化机制可以让对象不依附于程序独立存在。二、应用场景
Wesley13 Wesley13
2年前
FastJson、Jackson、Gson进行Java对象转换Json的细节处理
Java对象转换Json的细节处理前言Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?对比以下json转换方式一、fastJson1、fastJson在转换java对象为json的时候,默认是不序列化nu
Stella981 Stella981
2年前
Redis 自定义对象 cannot be cast to java.lang.String
Redis序列化对象的时候报错如下java.lang.ClassCastException:com.ppdai.cbd.ddp.thirdparty.contract.bhxtzx.BHXTZXTaskcannotbecasttojava.lang.Stringatorg.springframework.d
红烧土豆泥 红烧土豆泥
2年前
解决Redis序列化Java8的LocalDateTime问题
在从Redis获取带有LocalDateTime类型属性的对象时,产生序列化和反序列化问题解决办法方式一:实体类上指定LocalDateTime的序列化器和反序列化器java@JsonDeserialize(usingLocalDateTimeDeserializer.class)//反序列化@JsonSerialize(usingLo
Stella981 Stella981
2年前
Android Serializable与Parcelable原理与区别
一、序列化、反序列化是什么?(1)名词解释对象的序列化:把Java(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.2cto.com%2Fkf%2Fware%2FJava%2F)对象转换为字节序列并存储至一个储存媒介的过
Wesley13 Wesley13
2年前
Java序列化——transient关键字和Externalizable接口
  提到Java序列化,相信大家都不陌生。我们在序列化的时候,需要将被序列化的类实现Serializable接口,这样的类在序列化时,会默认将所有的字段都序列化。那么当我们在序列化Java对象时,如果不希望对象中某些字段被序列化(如密码字段),怎么实现呢?看一个例子:import java.io.Serializable;imp
Wesley13 Wesley13
2年前
Java并发编程:Java 序列化的工作机制
JDK内置同步器的实现类经常会看到java.io.Serializable接口,这个接口即是Java序列化操作,这样看来序列化也是同步器的一种机制。 关于序列化本文主要分析Java中的序列化机制,并看看AQS同步器的序列化,掌握序列化机制才能完整理解JDK内置的同步工具的实现。在程序中为了能直接以Java对象的形式进行保存,然后再