CacheManager设置Jackson2JsonRedisSerializer的时候不生效问题

CacheManager设置Jackson2JsonRedisSerializer的时候不生效问题

不生效的代码

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory, CacheProperties cacheProperties) {
        RedisCacheConfiguration config = redisCacheConfiguration(cacheProperties);
        RedisSerializer redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializer();

        config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

        RedisCacheManager cacheManager = RedisCacheManager
                .builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }

生效的代码

 @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory, CacheProperties cacheProperties) {
        RedisCacheConfiguration config = redisCacheConfiguration(cacheProperties);
        RedisSerializer redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializer();

        //卧槽,这个必须返回,因为他重新new了一个,坑死我了
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

        RedisCacheManager cacheManager = RedisCacheManager
                .builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }

你可能感兴趣的:(spring,数据库,数据仓库,数据挖掘)