SprignCloud整合Redis序列化问题【已解决】

项目中,redis作为一个单独的公共Moudle,被其他模块依赖

pom依赖:

        <!--redis集成-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

代码:

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 初始化基础数据
     * @return
     */
    @Override
    public R<String> initBasicData() {
        redisTemplate.opsForValue().set("HD-BASIC-USER","测试");
        Map test = new HashMap<>();
        test.put("hello","world");
        redisTemplate.opsForHash().put("HD-BASIC-MAP","TEST", JSONUtil.toJsonStr(test));
        return R.success("初始化成功");
    }
    
    /**
     * 获取基础数据
     * @return
     */
    @Override
    public R getBasicData() {
        String user = (String) redisTemplate.opsForValue().get("HD-BASIC-USER");
        log.info("-------------------------"+user);
        return R.data(redisTemplate.opsForHash().get("HD-BASIC-MAP","TEST"));
    }

初始化时存储数据到redis,检查redis时出现以下问题:

SprignCloud整合Redis序列化问题【已解决】

存储的String类型和HASH类型的数据,key前面出现了莫名其妙的编码,value也乱码了 

百度了一波,是序列化的问题,RedisTemplate的keySerializer和valueSerializer默认使用的是JdkSerializationRedisSerializer,debug看一下:

SprignCloud整合Redis序列化问题【已解决】

果然,那就换一个序列化的方式,将String类型的key和value以及HASH类型的key都用StringRedisSerializer来序列化,避免出现乱码的问题,将HASH类型的value序列化方式设置为jackson2JsonRedisSerializer来进行序列化。

上代码:

1.公共的redis模块新建配置对象,并初始序列化方式:

HdRedisConfig
package com.hd.redis.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
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;

import javax.annotation.Resource;

/**
 * @author liuch
 * @title: MyRedisConfig
 * @description: redis自定义序列化
 * @date 2021/8/6 9:51
 */
@Configuration
public class HdRedisConfig {

    @Resource
    RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<Object, Object> redisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

2.更新依赖后再测试,发现没有生效,是因为redis模块作为公共能提供依赖,自己项目在执行时,没有扫描到公共模块的自定义配置,

解决方法:

启动l类添加注解:

@ComponentScan(basePackages ="com.hd")

再次测试:

SprignCloud整合Redis序列化问题【已解决】

 搞定!

上一篇:就这?分布式 ID 发号器实战


下一篇:Day03安装jdk和HelloWorld!