问题描述:
redisTemplate.opsForValue().get()
拿任何一个值都是空,但是在redisDeskManager却都可以查的到, debug看了RedisTemplate 中的连接信息,发现确实是连接同一个db
结果发现是序列化的问题, 下面出我抄袭的redis配置代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisTemplateSerializer {
RedisTemplate redisTemplate;
@Autowired
public void setRedisTemplate(RedisTemplate redisTemplate) {
// 序列化为String
RedisSerializer stringSerializer = new StringRedisSerializer();
//序列化为Json
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
this.redisTemplate = redisTemplate;
}
}
一开始我只是把key做了StringRedisSerializer,但在查询的时候还是拿不到redis里面的数据
后来发现必须要把, key value都序列化才可以查得到