项目场景:
背景:在java中使用redis存储string类型的数据时,会出现乱码。
问题描述:
例如:在java中创建redistemplate操作string类型的数据存储时,会出现乱码,如下代码,
我写了一条存储key为name,值为虎哥的字符串。
然后获取一下这个key为name的值,打印得到的值
package com.sxy.redis; import org.junit.jupiter.api.test; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.data.redis.core.redistemplate; @springboottest class springdataredisdemoapplicationtests { @autowired private redistemplate redistemplate; @test void contextloads() { // 写入一条string数据 redistemplate.opsforvalue().set("name", "虎哥"); // 获取string数据 object name = redistemplate.opsforvalue().get("name"); system.out.println("name=" + name); } }
我们可以看到出现了乱码问题:这里不单单是值出现了乱码,而且key也有乱码问题
原因分析:
redis的序列化分析:
因为springdataredis的功能可以接收任何类型的对象,可以帮我们将java对象转成redis可处理的字节,所以我们存进去的 name和虎哥 都被当成了java对象,而redistemplate的底层,默认对这些对象得处理方式就是利用jdk的序列化工具,objectoutputstream
进入redistemplate我们发现了四个属性的值都是对应的数据的序列化
这里我们发现上面四个属性的值的,都是下面这个方法定义的默认的jdk的序列化器
通过断点调试redistemplate.opsforvalue().set("name", "虎哥");我们可以发现,进入了jdkserializationredisserializer的类中
最后进入了这里,可以看到有objectoutputstream来写对象,这个流的作用就是把java对象转换为字节进行writeobject进行写入
所以我们看到的值就是这样子的数据
缺点:
- 可读性差
- 内存占用较大
解决方案:
我们可以通过修改redistemplate的序列化来实现数据乱码问题。
redisserializer有一些实现类
这里会有两个我们需要用到的
- stringredisserializer
如果我的key和hash可以是字符串的情况下就用它
- genericjackson2jsonredisserializer
如果我的值是对象就用它
上代码:
代码如下,写了一个配置类注册bean对象给spring容器管理
package com.sxy.redis.config; 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.genericjackson2jsonredisserializer; import org.springframework.data.redis.serializer.redisserializer; @configuration public class redisconfig { @bean public redistemplate<string,object> redistemplate(redisconnectionfactory redisconnectionfactory){ // 创建redistemplate对象 redistemplate<string, object> template = new redistemplate<>(); // 设置连接工厂 template.setconnectionfactory(redisconnectionfactory); // 创建json序列化工具 genericjackson2jsonredisserializer genericjackson2jsonredisserializer = new genericjackson2jsonredisserializer(); // 设置key的序列化为 = string template.setkeyserializer(redisserializer.string()); template.sethashkeyserializer(redisserializer.string()); // 设置value的序列化为 = genericjackson2jsonredisserializer template.setvalueserializer(genericjackson2jsonredisserializer); template.sethashvalueserializer(genericjackson2jsonredisserializer); // 返回 return template; } }
注意:如果这里运行的话会报错为如下:
caused by: java.lang.noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
这是个明显的错误,告诉我们用了如下这个json序列化类没有引入jackson的依赖才报错
genericjackson2jsonredisserializer genericjackson2jsonredisserializer =
new genericjackson2jsonredisserializer();
我们只需要添加如下依赖即可
<!-- jackson依赖--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> </dependency>
随后我们尝试对其进行写入和读取数据,发现key和value都是正常的显示
总结:
直接在java中使用redistemplate模板进行写入字符串数据时会出现,key和value都是乱码问题,
我们修改了redistemplate默认的序列化器stringredisserializer即可解决此问题。
到此这篇关于java中redis存储string类型会有乱码的文章就介绍到这了,更多相关java redis存储乱码内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论