redis系列之踩坑:\xAC\xED\x00\x05t\x00\x08乱码问题

> 插: AI时代,程序员或多或少要了解些人工智能,前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站

坚持不懈,越努力越幸运,大家一起学习鸭~~~

问题:

在SpringBoot中使用RedisTemplate操作redis时,会出现\xAC\xED\x00\x05t\x00\x08这种乱码问题,特别是在使用中文时。

原因分析:

原因是因为RedisTemplate中key和value的序列化方式都默认使用了jdk的序列化方式,含有中文时就会出现乱码。想要解决乱码问题我们只需要将key和value的序列化方式分别修改为字符串和json的形式即可。

我们增加一个RedisConfig类,用来对RedisTemplate的序列化方式进行修改:

复制代码
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 设置key的序列化规则和value的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

问题解决,可以写几个单元测试方法测试一下。

相关推荐
kill bert3 小时前
Java八股文背诵 第四天JVM
java·开发语言·jvm
Pitayafruit5 小时前
📌 Java 工程师进阶必备:Spring Boot 3 + Netty 构建高并发即时通讯服务
spring boot·后端·netty
酱学编程5 小时前
redis 延迟双删
数据库·redis·缓存
梦想实现家_Z5 小时前
SpringBoot实现MCP Server实战详解
spring boot·后端·mcp
你是理想6 小时前
wait 和notify ,notifyAll,sleep
java·开发语言·jvm
helloworld工程师6 小时前
【微服务】SpringBoot整合LangChain4j 操作AI大模型实战详解
java·eclipse·tomcat·maven
Java&Develop6 小时前
idea里面不能运行 node 命令 cmd 里面可以运行咋回事啊
java·ide·intellij-idea
q567315236 小时前
使用Java的HttpClient实现文件下载器
java·开发语言·爬虫·scrapy
不知名美食探索家7 小时前
【11】Redis快速安装与Golang实战指南
redis·golang·bootstrap
遥不可及~~斌7 小时前
Spring Boot 项目日志系统全攻略:Logback、Log4j2、Log4j与SLF4J整合指南
spring boot·log4j·logback