向Redis中存储键值对时会出现,key:\xAC\xED\x00\x05t\x00\x1xxxxxxx value:\xAC\xED\x00\x05t\x00 xxxxxx 的情况:
原因:springboot默认使用JdkSerializationRedisSerializer序列化方式
解决:需要改成StringRedisSerializer或直接使用Jedis的方式
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { /** * redis模板,存储关键字是字符串,值是Jdk序列化 * @param factory * @return * @Description: */ @Bean public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); //key序列化方式;但是如果方法上有Long等非String类型的话,会报类型转换错误; RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息; redisTemplate.setKeySerializer(redisSerializer); redisTemplate.setHashKeySerializer(redisSerializer); //默认使用JdkSerializationRedisSerializer序列化方式;会出现乱码,改成StringRedisSerializer StringRedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); return redisTemplate; } }