@Configuration
public class RedisTemplateConfig {
@Autowired
public RedisTemplateConfig(RedisTemplate redisTemplate) {
//创建jackson的序列化方式
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
Object.class);
ObjectMapper objectMapper = new ObjectMapper();
//设置序列化时允许访问对象中的所有属性
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//json转化时保存类的信息
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY);
//修改jackson序列化的方式
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringKeySerializer = new StringRedisSerializer();
//修改key序列化为string序列化
redisTemplate.setKeySerializer(stringKeySerializer);
//将值得对象序列化转换为jackson序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//设置hashkey的序列化为string序列化
redisTemplate.setHashKeySerializer(stringKeySerializer);
//设置hashvalue的序列化为jackson序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//工厂创建redisTemplate对象之后在进行配置
redisTemplate.afterPropertiesSet();
}
}