posts - 46,  comments - 9,  views - 13万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

使用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 }
复制代码

 

posted on   十七年蝉  阅读(11021)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
点击右上角即可分享
微信分享提示