springboot redis获取的key为什么是乱码?


redisTemplate.keys() 获取的值怎么是乱码
redisconfig

public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {

RedisTemplate<Object, Object> template = new RedisTemplate<>();

template.setConnectionFactory(connectionFactory);

FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

ObjectMapper mapper = new ObjectMapper();

mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

serializer.setObjectMapper(mapper);

template.setValueSerializer(serializer);

// 使用StringRedisSerializer来序列化和反序列化redis的key值

template.setKeySerializer(new StringRedisSerializer());

template.afterPropertiesSet();

return template;

}


回答:

你为redis指定的kv序列化器使用的是什么类型?似乎是key序列化过程出了问题
这里有一个我配置redis的例子可以参考下,使用fastjson作为kv的序列化器

@Configuration

public class RedisConfig {

@Bean

public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactory(redisConnectionFactory);

FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);

redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);

redisTemplate.setValueSerializer(fastJsonRedisSerializer);

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setEnableDefaultSerializer(true);

return redisTemplate;

}

}

以上是 springboot redis获取的key为什么是乱码? 的全部内容, 来源链接: utcz.com/p/945391.html

回到顶部