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.(ObjectInputStream.java:301)
    at org.springframework.core.ConfigurableObjectInputStream.(ConfigurableObjectInputStream.java:65)
    at org.springframework.core.ConfigurableObjectInputStream.(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">
            class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        property>
        <property name="valueSerializer">
            class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        property>

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

你可能感兴趣的:(redis)