java代码redis中文乱码
时间: 2023-08-26 22:18:45 浏览: 239
根据引用和引用的内容,如果从共用的Redis中获取的数据是乱码,可能有两个原因。首先,模块C的默认字符集是GB2312,而Java的字符集是UTF-8,两边的编码不一致导致乱码。其次,模块C的同事封装的dll库,向Redis的列表中push的时候,不是push的字符串,而是byte数组。因此,你可以将Java代码中的字符集设置为GB2312来保持一致,或者尝试使用其他字符集进行适配。
另外,根据引用的内容,你可以通过改变RedisTemplate的默认序列化方式来解决中文乱码的问题。你可以将key的序列化方式改为StringRedisSerializer,将value的序列化方式改为Jackson2JsonRedisSerializer。你可以将以下代码放到Spring Boot的启动类中:
```
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(new ObjectMapper());
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
```
通过以上改变,你应该能够解决Java代码中Redis中文乱码的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [【Java】记一次通过Java从redis中获取json数据中的中文乱码问题的解决](https://blog.csdn.net/baidu_15338861/article/details/107859793)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [java存储redis数据类型以及客户端查看中文乱码的问题解决](https://blog.csdn.net/lifulian318/article/details/126699509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文