问题

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;
    }
}

tencent.jpg


clawhub
99 声望12 粉丝