美文网首页
SpringBoot设置RedisCache序列化报错

SpringBoot设置RedisCache序列化报错

作者: 谁家的猪 | 来源:发表于2019-07-29 17:48 被阅读0次

    配置RedisConfiguration后,序列化仍然报错,使用的还是JdkSerializationRedisSerializer

    1. 配置代码
    package com.istimeless.finance.paramcenter.configuration;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    import java.time.Duration;
    
    /**
     * @author lijiayin
     */
    @Configuration
    public class RedisConfiguration {
        
        @Autowired
        private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer;
        
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            return redisTemplate;
        }
    
        @Bean
        public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
            redisCacheConfiguration.serializeValuesWith(
                    RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
            redisCacheConfiguration.entryTtl(Duration.ofHours(12));
            RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
                    .builder(redisConnectionFactory).cacheDefaults(redisCacheConfiguration);
            return builder.build();
        }
        
        @Bean
        public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(){
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
                    new Jackson2JsonRedisSerializer<>(Object.class);
            jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
            return jackson2JsonRedisSerializer;
        }
    }
    
    1. 报错


      RedisCache序列化出错.png

    解决方案

    1. 错误原因
      查看RedisCacheConfiguration的serializeValuesWith方法
    public RedisCacheConfiguration serializeValuesWith(SerializationPair<?> valueSerializationPair) {
    
        Assert.notNull(valueSerializationPair, "ValueSerializationPair must not be null!");
    
        return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair,
                    valueSerializationPair, conversionService);
    }
    

    会重新生成RedisCacheConfiguration并返回,而我的配置方法中,并没有接收返回的类

    1. 解决方法
      将serializeValuesWith方法返回的类赋值给redisCacheConfiguration。

    相关文章

      网友评论

          本文标题:SpringBoot设置RedisCache序列化报错

          本文链接:https://www.haomeiwen.com/subject/lggbrctx.html