RedisCacheManager:JSON序列化、Cannot resolve constructor ‘RedisCacheManager‘

报错:
Cannot resolve constructor'RedisCacheManager(orgspringframeworkdata.reds.core.RedisTemplatesjava.lang.Object,com.demo.webdemo.pojo.User>"

springboot2.x对RedisCacheManager发生了改动,RedisCacheManager的单个参数不再适用。

因此,想要对进行json对象序列化,请看如下代码

@Bean
   public CacheManager redisCacheManager(RedisConnectionFactory factory){
    
    


      Jackson2JsonRedisSerializer<User> user = new Jackson2JsonRedisSerializer<>(User.class);

      RedisCacheConfiguration cacheManager =
              RedisCacheConfiguration.defaultCacheConfig()
                      //设置缓存有效时间(1小时)
                      .entryTtl(Duration.ofHours(1))
                      //不缓存null结果,若出现null结果时会报异常
                      .disableCachingNullValues()
                      //以json形式序列化对象
                      .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(user));


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

本文参考于:springboot 2.2.X 自定义CacheManager,JSON序列化配置

猜你喜欢

转载自blog.csdn.net/m0_46267375/article/details/108501896
今日推荐