RedisTemplate: Failed to deserialize payload
Posted ClawHub的技术分享
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RedisTemplate: Failed to deserialize payload相关的知识,希望对你有一定的参考价值。
问题
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)
原因分析
在使用org.springframework.data.redis.core.RedisTemplate.boundHashOps的increment方法后,调用其entries方法反序列化时出错。
主要是因为当执行increment方法时,对于入参中的hashVal没有进行序列化,直接调用了redis方法,当获取值时,走了默认的org.springframework.data.redis.serializer.JdkSerializationRedisSerializer,反序列化失败。
解决办法就是将HashValue的序列化方法改为org.springframework.data.redis.serializer.StringRedisSerializer,问题解决!
但是也相应的引入新的问题:所有的HashValue都会受到影响,值都必须为String。所幸Redis底层存储的值都是字符串,所以也无伤大雅。
解决办法
@Configuration
public class LimitRedisConfig {
@Bean
public RedisTemplate<Object, Object> limitRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
以上是关于RedisTemplate: Failed to deserialize payload的主要内容,如果未能解决你的问题,请参考以下文章
maven打包出错: Failed to clean project: Failed to delete
GlideException: Failed to load resource……java.net.SocketTimeoutException(failed to connect to xxx)
GlideException: Failed to load resource……java.net.SocketTimeoutException(failed to connect to xxx)
Android Studio 3.0预览版本 Error:Gradle: failed to create directory 解决方案