spring redis乱码前缀 \xAC\xED\x00\x05t\x00\x05

  1. @Bean(name = "redisTemplate")
  2. @ConditionalOnClass(RedisOperations.class)
  3. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  4. RedisTemplate<String, Object> template = new RedisTemplate<>();
  5. template.setConnectionFactory(factory);
  6. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
  7. ObjectMapper mapper = new ObjectMapper();
  8. mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  9. mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
  10. jackson2JsonRedisSerializer.setObjectMapper(mapper);
  11. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  12. template.setKeySerializer(stringRedisSerializer);
  13. template.setHashKeySerializer(stringRedisSerializer);
  14. template.setValueSerializer(jackson2JsonRedisSerializer);
  15. template.setHashValueSerializer(jackson2JsonRedisSerializer);
  16. template.afterPropertiesSet();
  17. return template;
  18. }