redis字符编码问题

今天遇到一个问题,从redis取出字符串数据后,想对全角的空格进行转换为半角空格,结果怎么都不行,后来对入口进行空格全角转半角结果成功,

分析原因,可能是从redis取出后对其进行了改变,导致。


需求说明:

redis做为一个数据缓存结构


思考:

在源头对数据进行过滤处理


### 解决 Redis 中出现的乱码问题 Redis 存储的数据如果未经过适当处理可能会导致乱码现象,这通常是因为存储对象时没有正确设置序列化方式所致。以下是针对该问题的具体解决方案。 #### 1. 默认配置下的序列化机制 默认情况下,`RedisTemplate` 使用 `JdkSerializationRedisSerializer` 进行序列化和反序列化操作[^2]。这种序列化器会将 Java 对象转换为字节数组形式保存到 Redis 中,而这些字节流在 Redis 客户端查看时表现为不可读的字符编码(如 `\xAC\xED\x00\x05t...`)。因此,为了防止乱码,可以更改序列化策略。 --- #### 2. 更改字符串键值对的序列化方式 对于简单的字符串键值对场景,推荐使用 `StringRedisSerializer` 来替代默认的序列化器。通过修改 `RedisTemplate` 的配置实现: ```java 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.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); // 设置key采用StringRedisSerializer template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); // 设置value采用Jackson2JsonRedisSerializer或其他适合的序列化器 template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); template.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); template.setConnectionFactory(factory); return template; } } ``` 上述代码中设置了 Key 和 Hash Key 使用 `StringRedisSerializer`,并为 Value 及其哈希结构中的字段指定了 JSON 序列化工具 `Jackson2JsonRedisSerializer`[^3]。 --- #### 3. 配置自定义序列化器 当需要支持复杂类型的对象存储时,可以选择其他更灵活的序列化方案,比如基于 JSON 或 Protobuf 的序列化方法。这里以 JSON 方式为例展示如何调整配置: ##### (a) 添加依赖项 确保项目已引入必要的库文件: ```xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> ``` ##### (b) 自定义序列化逻辑 利用 `GenericJackson2JsonRedisSerializer` 实现通用化的 JSON 转换功能: ```java template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); ``` 这种方式能够自动识别并适配多种数据类型,从而减少手动干预的需求。 --- #### 4. 测试验证效果 完成以上改动之后重新运行测试案例来确认修复成果: ```java package com.zzyl.test; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; @SpringBootTest public class RedisTest1 { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test public void testRedis() { redisTemplate.opsForValue().set("BB", "b"); redisTemplate.opsForHash().put("AA", "aa", 11); System.out.println(redisTemplate.opsForValue().get("BB")); // 输出应为 'b' System.out.println(redisTemplate.opsForHash().entries("AA")); // 输出应包含 {'aa': 11} } } ``` 此时再观察 Redis 数据内容就不会存在无法辨识的二进制串了。 --- ### 总结 通过对 `RedisTemplate` 的序列化选项做出合理设定即可有效规避因不当序列化引发的乱码难题。具体而言,可选用更适合业务需求的序列化手段代替原始 JDK 提供的方式;而对于纯文本交互则优先考虑运用 `StringRedisSerializer` 类型简化流程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值