SpringBoot配置Redis序列化规则,防止乱码,0xf或乱码

SpringBoot配置Redis序列化规则,防止乱码

SpringBoot引入Redis很简单,添加以下注解:

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

然后在application.yml中添加Redis配置:

spring:
  redis:
    host: localhost
    database: 1
    port: 6379

注:此处有一些额外的配置,比如password等,我这里为省事就不写了。

下面我们可以编写测试类了:

package com.eknown;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangfanghao
 * @version 1.0
 * @date 2019-07-21 00:58
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTest {

    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void test() {
        redisTemplate.opsForValue().set("test:1", "1233");
        //Object obj = redisTemplate.opsForValue().get("test:1");
        System.out.println("success");
    }
}

测试后发现一个问题,test:1这样的key,乱码了。比如我用Another.Redis.Desktop.Manager工具去查看时,发现变成了一串奇奇怪怪的字符串。

乱码了。

这是因为Redis默认序列化规则导致的。RedisTemplate默认的所有序列化规则都是JDKSerializer,而StringRedisTemplate默认的序列化规则是StringRedisSerializer。具体可以看下图:

[外链图片转存失败(img-a1Lkjpzl-1563879008220)(配置Redis序列化规则.assets/image-20190721225054441.png)]

我们需要对它进行配置:

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的序列化规则可以参考进行设置。

posted @ 2020-07-17 10:02  山河永慕~  阅读(1294)  评论(0编辑  收藏  举报