赞
踩
问题描述:
使用RedisTemplate(spring-data-redis )进行redis操作的封装 , 现有一个incr的key , 当调用incr后返回值一切正常, 当对此key进行get调用的时候出现了如下的异常:
- 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
- at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:42)
- at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:274)
- at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:52)
- at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:185)
- at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)
- at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:86)
- at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:43)
- at org.springframework.data.redis.core.DefaultBoundValueOperations.get(DefaultBoundValueOperations.java:42)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
- at java.lang.reflect.Method.invoke(Unknown Source)
- at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
- at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
- at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
- at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
- at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
- at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
- at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
-
- 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
- at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:61)
- at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:1)
- at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:40)
- ... 38 more
- Caused by: java.io.EOFException
- at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
- at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
- at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
- at java.io.ObjectInputStream.<init>(Unknown Source)
- at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:38)
- at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:58)
- ... 40 more
可以看出是sping对redis查询的返回结果进行deserialize的时候出错了 , 当然异常的提示也很明确:序列号对象生成这个字节数组的方法是否与默认的反序列化方法相对应;应该就是对称性吧 , 你用A方法对B进行序列号然后用不对称的反序列化方法C进行反序列号 , 肯定会有问题 , 结果在国外大神的回复中找到了答案:
提简单来说调用incr后得到 值不会出错是没有经过redistemplate的deserialize, 而get必须经过 , 所以只要设置redistemplate的ValueSerializer即可:
redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
补充说明: 经过几天的学习发现这个异常只需要配置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导致的,其它同理
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。