当前位置: 首页 > news >正文

RedisTemplate乱码问题

其实这是在解决一个项目问题是发现的,因为原开发者的大意,造成了系统出现严重的逻辑问题。

因为系统系统采用分模块开发,某模块使用Spring提供的RedisTemplate进行值的读写,另一位使用了框架基于Jedis的一套公用方法进行值的读写,出现的问题就是写入的值,后面读取不到进而进行了无值判断逻辑。

那么是读取不到吗,我使用RDM客户端查看,发现出现了一堆键值都是乱码的内容

然后在代码中加入以下代码:

package com.example.springboot.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate redisTemplate = new RedisTemplate();redisTemplate.setConnectionFactory(redisConnectionFactory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);//重点在这四行代码redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}
}

再存入redis就对了

http://www.lryc.cn/news/230832.html

相关文章:

  • Java用户和内核交互图
  • 2023.11.14使用bootstrap制作一个简洁的前端注册登录页
  • Avatar虚拟形象解决方案,趣味化的视频拍摄与直播新体验
  • MongoDB备份与恢复以及导入导出
  • 如何挑选猫主食罐头?宠物店自用的5款猫主食罐头推荐!
  • 立哥先进研发-API安全方案
  • 小函数:Lambda表达式(Java篇)
  • RSS订阅快速连接Notion
  • VMware ubuntu 新虚拟机的创建
  • 第一篇 《随机点名答题系统》简介及设计流程图(类抽奖系统、在线答题系统、线上答题系统、在线点名系统、线上点名系统、在线考试系统、线上考试系统)
  • C# .NET6 Log4net输出日志
  • python数据结构与算法-03_链表
  • Springboot-aop的使用
  • 数列计算
  • 阿里云全球故障凸显“云集中”风险
  • 【2015年数据结构真题】
  • vxe表格行拖拽
  • Linux之基本指令操作
  • 海康设备、LiveNVR等通过GB35114国密协议对接到LiveGBS GB28181/GB35114平台的详细操作说明
  • BUUCTF 面具下的flag 1
  • ArcGIS实现矢量区域内所有要素的统计计算
  • 3.4-初识Container
  • 壹基金爱泽瑞金 安全家园物料配送忙
  • arcgis--二维建筑面的三维显示设置
  • Maven 插件统一修改聚合工程项目版本号
  • 主从复制和读写分离
  • Redis模块的高级使用方式
  • Failed to restart network.service: Unit network.service not found.
  • wiki.js一个开源知识库系统
  • 关于Java抽象类和接口的总结和一点个人的看法