请教一下为啥我的 HashOperations<String, String, Integer> 的操作结果里会有一些奇怪的字符,能够去除吗?

2021-03-29 15:46:06 +08:00
 Newyorkcity

springboot data redis

@Configuration
public class RedisConfiguration {

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379));
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(@Autowired RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        return template;
    }

    @Bean
    public HashOperations<String, String, Integer> hashOperations(@Autowired RedisTemplate<String, Object> template) {
        return template.opsForHash();
    }
}

键应该是干脆利落的 message:friend:4,但是结果是 \xac\xed\x00\x05t\x00\x10message:friend:4

前面那几个东西看着可太烦了

626 次点击
所在节点    问与答
3 条回复
tcfenix
2021-03-29 15:53:41 +08:00
其实你用前面一串乱码搜索就能找到很多人的回答了, 这个就是 spring data 的序列化配置问题
canbingzt
2021-03-29 16:17:27 +08:00
RedisTemplate 设置一下 key 和 vlue 的序列化方法
Newyorkcity
2021-03-29 16:27:59 +08:00
谢谢几位 我再搜索了下 改成这样基本就好了


@Bean
public RedisTemplate<String, Object> redisTemplate(@Autowired RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setDefaultSerializer(new StringRedisSerializer());
return template;
}


就是这样的话,HashOperations 的泛型必须是 HashOperations<String, String, String>。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/766183

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX