springboot(22)修改RedisTemplate序列化工具

使用其他序列化替代jdk序列化

@Configuration
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory connectionFactory;

    @Primary
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        // 使用其他序列化替代jdk序列化
        RedisSerializer redisSerializer = getRedisSerializer();
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(redisSerializer);
        template.setValueSerializer(redisSerializer);
        template.setHashKeySerializer(redisSerializer);
        template.setHashValueSerializer(redisSerializer);
        template.afterPropertiesSet();
        return template;
    }

     private RedisSerializer getRedisSerializer(){
        // jackson序列化
//      Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//      ObjectMapper om = new ObjectMapper();
//      om.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
//      om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//      jackson2JsonRedisSerializer.setObjectMapper(om);
        // fastjson序列化
        return new GenericFastJsonRedisSerializer();
    }
}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(springboot)