V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Newyorkcity
V2EX  ›  问与答

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

  •  
  •   Newyorkcity · 2021-03-29 15:46:06 +08:00 · 611 次点击
    这是一个创建于 1096 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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

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

    3 条回复    2021-03-29 16:27:59 +08:00
    tcfenix
        1
    tcfenix  
       2021-03-29 15:53:41 +08:00   ❤️ 1
    其实你用前面一串乱码搜索就能找到很多人的回答了, 这个就是 spring data 的序列化配置问题
    canbingzt
        2
    canbingzt  
       2021-03-29 16:17:27 +08:00   ❤️ 1
    RedisTemplate 设置一下 key 和 vlue 的序列化方法
    Newyorkcity
        3
    Newyorkcity  
    OP
       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>。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4829 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.