服务器之家

服务器之家 > 正文

Redis缓存-序列化对象存储乱码问题的解决

时间:2021-08-11 19:05     来源/作者:十七年蝉

使用redis缓存对象会出现下图现象:

Redis缓存-序列化对象存储乱码问题的解决

键值对都是乱码形式。

解决以上问题:

如果是xml配置的

我们直接注入官方给定的keyserializer,valueserializer,hashkeyserializer即可:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<bean id="apiredistemplate" class="org.springframework.data.redis.core.redistemplate"
        p:connection-factory-ref="apicacheredisconnectionfactory">
        <property name="keyserializer">
            <bean
                class="org.springframework.data.redis.serializer.genericjackson2jsonredisserializer" />
        </property>
        <property name="valueserializer">
            <bean
                class="org.springframework.data.redis.serializer.genericjackson2jsonredisserializer" />
        </property>
 
        <property name="hashkeyserializer">
            <bean
                class="org.springframework.data.redis.serializer.genericjackson2jsonredisserializer" />
        </property>
        <property name="hashvalueserializer">
            <bean
                class="org.springframework.data.redis.serializer.genericjackson2jsonredisserializer" />
        </property>
        <property name="stringserializer">
            <bean
                class="org.springframework.data.redis.serializer.stringredisserializer" />
        </property>
    </bean>

spring boot 项目配置redisconfig的时候使用以下方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@configuration
public class redisconfig {
    @bean("jsonredistemplate")
    public redistemplate<object, object> redistemplate(redisconnectionfactory redisconnectionfactory)
            throws unknownhostexception {
        redistemplate<object, object> template = new redistemplate<object, object>();
        template.setconnectionfactory(redisconnectionfactory);      //解决日期序列化问题
        objectmapper om = new objectmapper();
        om.setdateformat(new simpledateformat("yyyy-mm-dd hh:mm:ss"));
        genericjackson2jsonredisserializer genericjackson2jsonredisserializer = new genericjackson2jsonredisserializer(om);
        template.setdefaultserializer(genericjackson2jsonredisserializer);
        return template;
 
    }
}

redis存入中文,取出来是乱码wenti

默认情况下,用redis存入中文,取出时会出现乱码情况,如图:

Redis缓存-序列化对象存储乱码问题的解决

解决

我们再启动redis时,可以在redis-cli 后面加上 --raw,如图

Redis缓存-序列化对象存储乱码问题的解决

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/zhuzhen/p/8125793.html

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部