当前位置:   article > 正文

RedisTemplate SerializationFailedException: Failed to deserialize payload 异常解决_attempt to deserialize unauthorized class

attempt to deserialize unauthorized class

问题描述:

   使用RedisTemplate(spring-data-redis )进行redis操作的封装 , 现有一个incr的key , 当调用incr后返回值一切正常, 当对此key进行get调用的时候出现了如下的异常:

   

  1. 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.EOFException
  2. at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:42)
  3. at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:274)
  4. at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:52)
  5. at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:185)
  6. at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)
  7. at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:86)
  8. at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
  9. at org.springframework.data.redis.core.DefaultBoundValueOperations.get(DefaultBoundValueOperations.java:42)
  10. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  11. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  12. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  13. at java.lang.reflect.Method.invoke(Unknown Source)
  14. at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
  15. at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
  16. at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
  17. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
  18. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
  19. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
  20. at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
  21. 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.EOFException
  22. at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:61)
  23. at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:1)
  24. at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:40)
  25. ... 38 more
  26. Caused by: java.io.EOFException
  27. at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
  28. at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
  29. at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
  30. at java.io.ObjectInputStream.<init>(Unknown Source)
  31. at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:38)
  32. at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:58)
  33. ... 40 more
   可以看出是sping对redis查询的返回结果进行deserialize的时候出错了 , 当然异常的提示也很明确:序列号对象生成这个字节数组的方法是否与默认的反序列化方法相对应;应该就是对称性吧 , 你用A方法对B进行序列号然后用不对称的反序列化方法C进行反序列号 , 肯定会有问题 , 结果在国外大神的回复中找到了答案:

     

        提简单来说调用incr后得到 值不会出错是没有经过redistemplate的deserialize, 而get必须经过 , 所以只要设置redistemplate的ValueSerializer即可:

       

    redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));

补充说明: 经过几天的学习发现这个异常只需要配置RedisTemplate的几个属性就可以了:
	
  1. <property name="keySerializer">
  2. <bean
  3. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  4. </property>
  5. <property name="valueSerializer">
  6. <bean
  7. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  8. </property>


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

     


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/1018296
推荐阅读
相关标签
  

闽ICP备14008679号