玩转springboot之springboot集成redis乱码问题

springboot集成redis乱码问题

在使用redis操作字符串时发现会变成乱码,这是因为RedisTemplate默认是使用的是JdkSerializationRedisSerializer序列化方式,这里可以使用StringRedisTemplate来进行操作,StringRedisTemplate中默认使用的是StringRedisSerializer

也可以对RedisTemplate进行配置,设置其序列化方式

复制代码
@Configuration
public class RedisConfig {

    // 配置redisTemplate
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        // 设置key的序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // value的序列化
        Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        redisTemplate.setValueSerializer(jsonRedisSerializer);

        return redisTemplate;
    }
}

https://zhhll.icu/2022/框架/springboot/集成/1.springboot集成redis乱码问题/
© 著作权归作者所有,转载或内容合作请联系作者

喜欢的朋友记得点赞、收藏、关注哦!!!

相关推荐
哈喽姥爷6 分钟前
Spring Boot---自动配置原理和自定义Starter
java·spring boot·后端·自定义starter·自动配置原理
阿拉斯加大闸蟹44 分钟前
基于RDMA 通信的可负载均衡高性能服务架构
运维·架构·负载均衡
小蒜学长1 小时前
基于springboot 校园餐厅预约点餐微信小程序的设计与实现(代码+数据库+LW)
数据库·spring boot·微信小程序
清风徐来QCQ1 小时前
阿里云centos7-mysql的使用
mysql·阿里云·云计算
不是吧这都有重名1 小时前
为什么ubuntu大文件拷贝会先快后慢?
linux·运维·ubuntu
Rhys..2 小时前
Python&Flask 使用 DBUtils 创建通用连接池
开发语言·python·mysql
舒一笑2 小时前
为什么where=Version就是乐观锁了?
后端·mysql·程序员
sunshine-sm2 小时前
CentOS Steam 9安装 Redis
linux·运维·服务器·redis·centos
小熊h2 小时前
MySQL集群高可用架构——组复制 (MGR)
linux·数据库·mysql