项目最初的序列化方案用的是JDK序列化类,但保存到redis里会产生乱码不方便查看管理。
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) { redisTemplate.setKeySerializer(new JdkSerializationRedisSerializer(Object.class.getClassLoader())); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer(Object.class.getClassLoader())); redisTemplate.setHashKeySerializer(new JdkSerializationRedisSerializer(Object.class.getClassLoader())); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer(Object.class.getClassLoader())); this.redisTemplate = redisTemplate; }
经过上网查看是由于序列化类的问题,我们不用jdk的序列化方法,采用json的格式
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); this.redisTemplate = redisTemplate; }
采用上述序列化器时需保证实体类里的所有get方法都有属性字段,否则反序列话时会出现问题,若没有需要加@JsonIgnore注解,序列化时会进行忽略。
SpringBoot引入Redis很简单,添加以下注解:
然后在application.yml中添加Redis配置:
注:此处有一些额外的配置,比如password等,我这里为省事就不写了。
测试后发现一个问题,test:1这样的key,乱码了。比如我用
Another.Redis.Desktop.Manager工具去查看时,发现变成了一串奇奇怪怪的字符串。
乱码了。
这是因为Redis默认序列化规则导致的。RedisTemplate默认的所有序列化规则都是JDKSerializer,而StringRedisTemplate默认的序列化规则是StringRedisSerializer。
package com.eknown.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis配置 * 主要是配置Redis的序列化规则,用Jackson2JsonRedisSerializer替换默认的jdkSerializer * @author zhangfanghao * @version 1.0 * @date 2019-07-21 21:04 */ @Configuration public class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); // 使用Jackson2JsonRedisSerialize替换默认序列化 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 设置key和value的序列化规则 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
注:此处仅重新设置了Key和Value的序列化规则,hash-key和hash-value的序列化规则可以参考进行设置。
以上是SpringBoot下Redis序列化乱码如何解决的详细内容。更多信息请关注PHP中文网其他相关文章!