使用redisTemplate操作redis默认使用JdkSerializationRedisSerializer解析器会导致命名空间为二进制码。文章来源地址https://www.toymoban.com/news/detail-546870.html
/**
* 设备redis解析器
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
StringRedisSerializer keyStringRedisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());//处理model中的时间字段
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setEnableTransactionSupport(false);//设置成true后在redis集群模式下事务失败
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(keyStringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(keyStringRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
文章来源:https://www.toymoban.com/news/detail-546870.html
到了这里,关于redis redisTemplate解析器(JdkSerializationRedisSerializer、StringRedisSerializer)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!