import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configurable class RedisTemplateConfig { @Autowired public RedisTemplateConfig(RedisTemplate redisTemplate){ // 1. 创建jackson序列化方式 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( Object.class); // 2. 创建object mapper ObjectMapper objectMapper = new ObjectMapper(); // 允许访问对象的所有属性 objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 转换json过程中保存类信息 objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // 设置value的序列化规则和key的序列化规则 redisTemplate.setKeySerializer(stringRedisSerializer); // jackson2JsonRedisSerializer 就是JSON的序列号规则 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // 设置hash类型key/value序列化 redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // 工厂创建redisTemplate对象之后在进行配置 redisTemplate.afterPropertiesSet(); } }