redis 反序列化deserialize异常问题解决

6 篇文章 0 订阅
1 篇文章 0 订阅

异常信息

 

ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is org.springframework.core.NestedIOException: Failed to deserialize object type; nested exception is java.lang.ClassNotFoundException: com.xxx.tpam.dao.model.Dict] with root cause

java.lang.ClassNotFoundException: com.xxx.tpam.dao.model.Dict

分析:这个错误提示找不到类,反序列化的时候报错,而对象系列化时是跟对象所处的包路径相关的。

 

我本地测试,几个系统用的同一个redis,对象在一个系统中序列化存入redis,包路径com.xxx.tpam.dao.model.Dict;主要原因是因为key也是相同,在运行另一个系统时,发现这个key已经存在了,取出时,因当前这个系统包路径为com.xxx.book.dao.model.Dict,由于两个对象的类所处的包路径不一致,反序列化失败,因而报找不到类错误。

解决方法:

直接删除redis中对应的值即可

我是使用的本地库,就直接在redis-cli 执行了flushall(删除所有数据库中的所有key ) 命令

 

 

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Redis的官方未提供Hessian序列化反序列化模块,但可以通过使用开源的Java Hessian库实现Hessian序列化与反序列化。使用步骤如下: 1. 下载Java Hessian库,将jar包添加到Java项目的classpath中。 2. 创建Hessian序列化工具类,将对象序列化成字节数组: ``` import com.caucho.hessian.io.HessianInput; import com.caucho.hessian.io.HessianOutput; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class HessianSerializer { public static byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); HessianOutput output = new HessianOutput(baos); output.writeObject(obj); return baos.toByteArray(); } public static Object deserialize(byte[] bytes) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); HessianInput input = new HessianInput(bais); return input.readObject(); } } ``` 3. 在Redis中设置Hessian序列化: ``` import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; public class HessianRedisSerializer implements RedisSerializer<Object> { @Override public byte[] serialize(Object obj) throws SerializationException { if (obj == null) { return new byte[0]; } try { return HessianSerializer.serialize(obj); } catch (IOException e) { throw new SerializationException("Hessian serialize error", e); } } @Override public Object deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length == 0) { return null; } try { return HessianSerializer.deserialize(bytes); } catch (IOException e) { throw new SerializationException("Hessian deserialize error", e); } } } ``` 4. 在Redis配置文件中设置Hessian序列化: ``` spring.redis.serializer=com.example.redisserializer.HessianRedisSerializer ``` 以上就是Redis实现Hessian序列化反序列化的简单实现方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值