Redis缓存 序列化对象存储乱码问题

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

键值对都是乱码形式。

解决以上问题:

如果是xml配置

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

 1 <bean id="apiRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
 2         p:connection-factory-ref="apiCacheRedisConnectionFactory">
 3         <property name="keySerializer">
 4             <bean
 5                 class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
 6         </property>
 7         <property name="valueSerializer">
 8             <bean
 9                 class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
10         </property>
11 
12         <property name="hashKeySerializer">
13             <bean
14                 class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
15         </property>
16         <property name="hashValueSerializer">
17             <bean
18                 class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
19         </property>
20         <property name="stringSerializer">
21             <bean
22                 class="org.springframework.data.redis.serializer.StringRedisSerializer" />
23         </property>
24     </bean>

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

 1 @Configuration
 2 public class RedisConfig {
 3     @Bean("jsonRedisTemplate")
 4     public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
 5             throws UnknownHostException {
 6         RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
 7         template.setConnectionFactory(redisConnectionFactory);
      //解决日期序列化问题
8 ObjectMapper om = new ObjectMapper(); 9 om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")); 10 GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om); 11 template.setDefaultSerializer(genericJackson2JsonRedisSerializer); 12 return template; 13 14 } 15 }

 

转载于:https://www.cnblogs.com/zhuzhen/p/8125793.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值