redisConfig 笔记
作者:互联网
@Bean(name = "redisTemplate") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; }
在使用redis 存储数据的时候,我们需要存储json 数据类型,但是在配置方法中加入了
//om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);后进入redis的就是序列化对象字符串了.注意ObectMapper 这个类的使用
标签:om,redisConfig,笔记,jackson2JsonRedisSerializer,template,new,序列化,ObjectMapper 来源: https://www.cnblogs.com/wangyaxin/p/16183192.html