| | |
| | | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| | | import com.fasterxml.jackson.annotation.PropertyAccessor; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.terra.common.entity.all.FastJson2JsonRedisSerializer; |
| | | import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| | | import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
| | | import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; |
| | | import org.springframework.cache.Cache; |
| | | import org.springframework.cache.annotation.CachingConfigurerSupport; |
| | |
| | | * @author WWW |
| | | */ |
| | | @Configuration |
| | | @AutoConfigureAfter(RedisAutoConfiguration.class) |
| | | @SuppressWarnings("ALL") |
| | | @AutoConfigureBefore(RedisAutoConfiguration.class) |
| | | public class RedisConfig extends CachingConfigurerSupport { |
| | | /** |
| | | * 配置自定义redisTemplate |
| | | */ |
| | | @SuppressWarnings("deprecation") |
| | | @Bean |
| | | public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { |
| | | @SuppressWarnings(value = { "unchecked", "rawtypes" }) |
| | | public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { |
| | | RedisTemplate<String, Object> template = new RedisTemplate<>(); |
| | | template.setConnectionFactory(redisConnectionFactory); |
| | | template.setConnectionFactory(factory); |
| | | |
| | | // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 |
| | | Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class); |
| | | //Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); |
| | | FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); |
| | | |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| | | mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
| | | serializer.setObjectMapper(mapper); |
| | | |
| | | template.setValueSerializer(serializer); |
| | | |
| | | //使用StringRedisSerializer来序列化和反序列化redis的key值 |
| | | template.setKeySerializer(new StringRedisSerializer()); |
| | | template.setHashKeySerializer(new StringRedisSerializer()); |
| | | template.setHashValueSerializer(serializer); |
| | | //template.setValueSerializer(new StringRedisSerializer()); |
| | | template.setValueSerializer(serializer); |
| | | template.afterPropertiesSet(); |
| | | |
| | | return template; |