Home >Database >Redis >How to solve Redis serialization garbled code under SpringBoot

How to solve Redis serialization garbled code under SpringBoot

王林
王林forward
2023-05-27 11:34:061925browse

Redis serialization garbled code under SpringBoot

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;
    }

Pay attention to the problem

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 configures Redis serialization rules to prevent garbled characters

SpringBoot introduces Redis very simply, add the following annotations:

How to solve Redis serialization garbled code under SpringBoot

Then in application. Add Redis configuration in yml:

How to solve Redis serialization garbled code under SpringBoot

Note: There are some additional configurations here, such as password, etc., I will not write them here to save trouble.

Now we can write the test class

How to solve Redis serialization garbled code under SpringBoot

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.

You can see the picture below for details

How to solve Redis serialization garbled code under SpringBoot

How to solve Redis serialization garbled code under SpringBoot

We need to configure it

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete