redis乱码

1.首先看乱码的样子



2.目前这个Key 在Redis中真实的样子



3.解决办法: 替换默认的序列化key 和value 的对象

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Created by sewen on 2017/9/12.
 */
@Configuration
public class Config {

    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public RedisTemplate redisTemplateInit() {
        //设置序列化Key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

4.尾巴:经过以上方法设置完成之后 使用 RedisTemplate set 时就不会乱码了.

public <T> ValueOperations<String, T> setCacheObject(String key, T value) {
    ValueOperations<String, T> operation = redisTemplate.opsForValue();
    operation.set(key, value);
    return operation;
}

扫描二维码关注公众号,回复: 355787 查看本文章

不正确的地方请多指教!


猜你喜欢

转载自blog.csdn.net/chrise_/article/details/79449596