• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

键值对都是乱码形式。

解决以上问题:

如果是xml配置的

我们直接注入官方给定的keySerializer,valueSerializer,hashKeySerializer即可:

<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的时候使用以下方法:

@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-cli 后面加上 --raw,如图

以上为个人经验,希望能给大家一个参考,也希望大家多多支持极客世界。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MySQL8.0升级的踩坑历险记发布时间:2022-02-08
下一篇:
mysql分表之后如何平滑上线详解发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap