Login
网站首页 > 文章中心 > 其它

Redis缓存_序列化对象存储乱码问题_redis存储对象为什么序列化

作者:小编 更新时间:2023-08-25 07:58:32 浏览量:484人看过

Redis缓存_序列化对象存储乱码问题_redis存储对象为什么序列化-图1

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

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 redisTemplate(RedisConnectionFactory redisConnectionFactory)
 5             throws UnknownHostException {
 6         RedisTemplate template = new RedisTemplate();
 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 }
版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章