使用redis作为缓存保存对象的时候出现错误,报错的信息如下:

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Cannot construct instance of `com.example.rediscachetest.entity.User` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (byte[])"{"@class":"com.example.rediscachetest.entity.User","id":1,"name":"user1"}"; line: 1, column: 52]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.rediscachetest.entity.User` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (byte[])"{"@class":"com.example.rediscachetest.entity.User","id":1,"name":"user1"}"; line: 1, column: 52]

	at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:152)
	at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:130)
	at org.springframework.data.redis.serializer.DefaultRedisElementReader.read(DefaultRedisElementReader.java:49)
	at org.springframework.data.redis.serializer.RedisSerializationContext$SerializationPair.read(RedisSerializationContext.java:272)
	at org.springframework.data.redis.cache.RedisCache.deserializeCacheValue(RedisCache.java:298)
	at org.springframework.data.redis.cache.RedisCache.lookup(RedisCache.java:95)
	at org.springframework.cache.support.AbstractValueAdaptingCache.get(AbstractValueAdaptingCache.java:58)
	at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:73)
	at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:571)
	at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:536)
	at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:402)
	at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:345)
	at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:64)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
	at com.example.rediscachetest.service.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$f960d3a2.get(<generated>)
	at com.example.rediscachetest.RedisCacheTestApplicationTests.getTwice(RedisCacheTestApplicationTests.java:22)

根据报错信息

Could not read JSON: Cannot construct instance of `com.example.rediscachetest.entity.User` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

是在反序列化的时候将JSON转成对象时出错,一开始会觉得是redisTemplate配置有问题,但是其它测试代码(写入对象到redis缓存时序列化是成功的),感觉可能是其它原因。经过在网上查找了解一些说法,以及再看报错信息里的no Creators, like default constructor, exist,感觉问题可能在于实体类的构造函数有问题。

检查代码,发现自己写了一个带所有参数的构造函数,按理应该没有问题,看了下报错信息的default constructor,尝试再添加一个无参构造函数,因为自己添加了有参的构造函数之后这个默认的午餐构造函数就没了,需要自己重新显示地写出来。测试一下,发现发序列化没有问题了。
看到有一篇文章说到:

redis的这些序列化方式,使用的是无参构造函数进行创建对象set方法进行赋值,方法中存在有参的构造函数,默认存在的无参构造函数是不存在的(继承自object),必须显示的去重写。

所以如果自己有添加了有参构造函数的话,还需要显示的去添加默认无参构造函数:

public User(){
}

原本想深入研究一下反序列化的过程,但是因为能力有限,暂时无法分析得很清楚,等后续继续更新。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐