1. 程式人生 > >關於修改springboot中redis配置中的修改RedisTemplate 預設的序列化規則(修改成JSON資料型別)

關於修改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;
}