zoukankan      html  css  js  c++  java
  • SpringBoot2.x配置JsonRedisSerializer

    @Configuration
    public class SpringCacheRedisConfig { @Bean public RedisCacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解决查询缓存转换异常的问题 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解决乱码的问题) RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(timeToLive) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; }

    //给redisTemplate配置json序列化
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setDefaultSerializer(RedisSerializer.json());
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    return redisTemplate;
    }
    } 链接:https://www.jianshu.com/p/9de4d4932e07

    这样就可以解决,使用redisTemplate或redisCacheManager操作k-v,导致key:x0x0x00x000key,value:乱码
    比如:
    Order order = orderMapper.selectOrderByOrderId(Long.parseLong(id));
    redisTemplate.opsForValue().set("testbrx",order);//
    redisTemplate1.opsForValue().set("asda",order);
    System.out.println(redisTemplate1.opsForValue().get("asda"));
    Cache cache = xcacheManager.getCache("h::1");
    if (cache!=null) {
    System.out.println(cache.getName());
    cache.put("2","asdasdasdasd");
    System.out.println(cache.get("2",String.class));
    cache.put("3",order);
    System.out.println(cache.get("3",Order.class));
    }
  • 相关阅读:
    Java的多线程机制系列:(二)缓存一致性和CAS
    Java的多线程机制系列:(一)总述及基础概念
    pydev常用快捷键
    利用python的pyinotify模块监控文件夹和文件的变动
    Linux 下文件名乱码(无效的编码)的解决办法
    ubuntu 中文界面下中文文件夹改英文
    SystemParametersInfo
    jsp:forward与缓冲区
    kafka:(9) 流式处理
    kafka:(8) 事务、幂等
  • 原文地址:https://www.cnblogs.com/brxHqs/p/10298233.html
Copyright © 2011-2022 走看看