专栏首页InvQ的专栏redis 反序列化deserialize异常问题解决

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

日志如下

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 java.io.StreamCorruptedException: invalid stream header: 4D797371
    at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:84)
    at org.springframework.data.redis.serializer.SerializationUtils.deserializeValues(SerializationUtils.java:54)
    at org.springframework.data.redis.serializer.SerializationUtils.deserialize(SerializationUtils.java:68)
    at org.springframework.data.redis.core.AbstractOperations.deserializeValues(AbstractOperations.java:268)
    at org.springframework.data.redis.core.DefaultListOperations.lambda$range$6(DefaultListOperations.java:171)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
    at org.springframework.data.redis.core.DefaultListOperations.range(DefaultListOperations.java:171)
    at redis.RedisTemplet.lGet(RedisTemplet.java:471)
    at RedisTest.testByXml(RedisTest.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.StreamCorruptedException: invalid stream header: 4D797371
    at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:78)
    at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:36)
    at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:82)
    ... 40 more
Caused by: java.io.StreamCorruptedException: invalid stream header: 4D797371
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:808)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:301)
    at org.springframework.core.ConfigurableObjectInputStream.<init>(ConfigurableObjectInputStream.java:65)
    at org.springframework.core.ConfigurableObjectInputStream.<init>(ConfigurableObjectInputStream.java:51)
    at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:70)
    at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:73)
    ... 42 more

可以看出是sping对redis查询的返回结果进行deserialize的时候出错了

然异常的提示也很明确:序列号对象生成这个字节数组的方法是否与默认的反序列化方法相对应;应该就是对称性吧 , 你用A方法对B进行序列化然后用不对称的反序列化方法C进行反序列号 , 肯定会有问题 。 结果在国外大神的回复中找到了答案:

by default,RedisTempate uses a JdkSerializationRedisSerializer,so if you did a ‘set’ it would make your Long look something like this in Redis: "\xac\xed\x00\xo5sr\x00\x0eejava.lang.Long;\x8b\xe4\90\xcc\x8f#\xdf\x02" IncrBy works because Redis always returns a Long from that operation ,so RedisTemplate does not attempt to deserialize the result.The result of “get” however,goes through the deserialization process,which expects a format like the above. you can solve this by using a difference value serializer on you RedisTemplate; RedisTemplate.setValueSerializer(new GenericToStringSerializer

简单来说调用incr后得到 值不会出错是没有经过redistemplate的deserialize, 而get必须经过 , 所以只要设置redistemplate的ValueSerializer即可: 只需要在spring-redis.xml文件中配置RedisTemplate的这两个属性就可以了(配置序列化器)

 <property name="keySerializer">
            <bean  class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>

这样不仅可以解决redis中key前面奇葩的字符形如:\xac\xed\x00\x05t\x00\x10 ,这就是默认keySerializer JdkSerializationRedisSerializer 导致的,其它同理

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • spring boot 启动报错 org/springframework/core/ErrorCoded

    新建了一个spring boot Junit 项目,跑着玩。。结果启动报错。 因为 SpringBoot提倡简化开发,不需要自己管理很多spring的依赖 ...

    MickyInvQ
  • java.lang.NoSuchMethodError: org.springframework.core.ResolvableType.forInstance 错误解决

    forInstance() method of ResolvableType class has been introduced in Spring 4.2. ...

    MickyInvQ
  • IP不能作为rocketmq 的topic

    MickyInvQ
  • RedisTemplate执行lua脚本,集群模式下报错解决

    在使用spring的RedisTemplate执行lua脚本时,报错EvalSha is not supported in cluster environmen...

    stys35
  • Redis一次Read time out引发的过期key删除策略分析

    一台业务CVM调用主从版的Redis偶发性的会出现客户端jedis抛出 SocketTimeoutException: Read time out 的报错,报错...

    elontian田凌翔
  • 又出生产事故!那些年我曾犯过的错-SecureRandom

    本人上周亲手写下了一个牛逼的bug,直接导致的结果是,晚上12点升级后台接口以后,第二天早上7点多开始,所有的app页面出现卡顿,白屏。

    黄泽杰
  • java.sql.SQLException: connection holder is null

    java.sql.SQLException: connection holder is null

    一个会写诗的程序员
  • 杨校老师课堂之Java基础集合专题知识点整理

    LinkedList是List的子类,List中的方法LinkedList都是可以使用,这里就不做详细介绍,我们只需要了解LinkedList 的特有方法即可。...

    杨校
  • There is no known eureka server; cluster server list is empty

    IT云清
  • DeepLearningNotes

    f(\frac{x_1+x_2}{2})\leq{\frac{f(x_1)+f(x_2)}{2}}

    DuncanZhou

扫码关注云+社区

领取腾讯云代金券