2

My spring stores session into redis, but the key is like "\xac\xed\x00\x05t\x00(spring:session". Now I want to remove the weird prefix and I know the solution is using StringRedisSerializer in RedisTemplate, but my config code below does not work:

@EnableRedisHttpSession()
@Configuration
public class SpringSessionConfig {

    @Autowired
    JedisConnectionFactory jedisConnectionFactory;

    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(StandardCharsets.UTF_8);

    @Bean
    public StringRedisTemplate redisTemplate() {
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(this.jedisConnectionFactory);
        redisTemplate.setDefaultSerializer(this.stringRedisSerializer);
        redisTemplate.setHashKeySerializer(this.stringRedisSerializer);
        redisTemplate.setKeySerializer(this.stringRedisSerializer);
        return redisTemplate;
    }
}

There are answers that using xml config file to do the same thing, but I prefer using pure java code, anyone can help with the above code? Thx.

seerhut
  • 21
  • 4
  • What does "does not work" mean? What error do you get? – dunni Apr 14 '16 at 14:04
  • @dunni It still add "\xac\xed\x00\x05t\x00" into key. – seerhut Apr 14 '16 at 14:20
  • And what is the key, which you provide to the RestTemplate and gets serialized? If that value contains bytes which would lead to the shown prefix, it would work perfectly normal. – dunni Apr 14 '16 at 14:47
  • Also, if you could post a link to such XML configurations, we could compare if your configuration is complete or is missing something. – dunni Apr 14 '16 at 14:48
  • I use spiring-session-redis to store session data, so the redis key is key in session, I think it is normal string. Some other post gave the xml config : [link](http://stackoverflow.com/questions/13215024/weird-redis-key-with-spring-data-jedis) . – seerhut Apr 17 '16 at 04:07
  • And I found another weird thing: I used to click "Run" button in Idea or Eclipse to launch the program before, and the "\xac.." will appear. Just now I tried to use `mvn spring-boot:run` in term to launch it, and the prefix disappeared! – seerhut Apr 17 '16 at 04:13

0 Answers0