The original serialization solution of the project used the JDK serialization class, but saving it to redis will produce garbled code, which is inconvenient to view and manage.
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; }
After checking on the Internet, it is due to the serialization class problem. We do not use jdk’s serialization method and use json format
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; }
Use the above serializer It is necessary to ensure that all get methods in the entity class have attribute fields, otherwise problems will occur when deserializing the conversation. If there is no need to add the @JsonIgnore annotation, it will be ignored during serialization.
SpringBoot introduces Redis very simply, add the following annotations:
Then in application. Add Redis configuration in yml:
Note: There are some additional configurations here, such as password, etc., I will not write them here to save trouble.
After testing, we found a problem, the key like test:1 was garbled. For example, when I used the
Another.Redis.Desktop.Manager tool to view it, I found that it turned into a series of strange strings.
is garbled.
This is caused by Redis default serialization rules. All the default serialization rules of RedisTemplate are JDKSerializer, and the default serialization rules of StringRedisTemplate are 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; } }
Note: Only the serialization rules of Key and Value are reset here. The serialization rules of hash-key and hash-value can be set by reference.
The above is the detailed content of How to solve Redis serialization garbled code under SpringBoot. For more information, please follow other related articles on the PHP Chinese website!