Newyorkcity
V2EX  ›  问与答

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

  •  
  •   Newyorkcity · Mar 29, 2021 · 1074 views
    This topic created in 1895 days ago, the information mentioned may be changed or developed.

    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 replies    2021-03-29 16:27:59 +08:00
    tcfenix
        1
    tcfenix  
       Mar 29, 2021   ❤️ 1
    其实你用前面一串乱码搜索就能找到很多人的回答了, 这个就是 spring data 的序列化配置问题
    canbingzt
        2
    canbingzt  
       Mar 29, 2021   ❤️ 1
    RedisTemplate 设置一下 key 和 vlue 的序列化方法
    Newyorkcity
        3
    Newyorkcity  
    OP
       Mar 29, 2021
    谢谢几位 我再搜索了下 改成这样基本就好了


    @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>。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3012 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 07:14 · PVG 15:14 · LAX 00:14 · JFK 03:14
    ♥ Do have faith in what you're doing.