关于修改springboot中redis配置中的修改RedisTemplate 默认的序列化规则(修改成JSON数据类型)

原理:覆盖默认配置类;
在创建的springboot项目中的启动类中:


public static void main(String[] args) {
SpringApplication.run(TestspringBoot04redisApplication.class, args);
}

//覆盖默认的自动配置
@Bean
public RedisTemplate<Object, Object> redisTemplate(
        RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    //修改默认的序列化规则
    //1.创建序列化规则对象
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
    //2.更改默认的序列化规则
    template.setDefaultSerializer(jackson2JsonRedisSerializer);
    return template;
}

猜你喜欢

转载自blog.csdn.net/ytt872496665/article/details/81414680