|
|
@@ -8,8 +8,10 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
8
|
8
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
9
|
9
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
10
|
10
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
11
|
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
|
11
|
12
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
12
|
13
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
14
|
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
|
13
|
15
|
|
|
14
|
16
|
/**
|
|
15
|
17
|
* redis配置
|
|
|
@@ -21,7 +23,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
21
|
23
|
public class RedisConfig extends CachingConfigurerSupport
|
|
22
|
24
|
{
|
|
23
|
25
|
@Bean
|
|
24
|
|
- @SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" })
|
|
|
26
|
+ @SuppressWarnings(value = { "unchecked", "rawtypes" })
|
|
25
|
27
|
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
|
|
26
|
28
|
{
|
|
27
|
29
|
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
|
|
@@ -31,12 +33,17 @@ public class RedisConfig extends CachingConfigurerSupport
|
|
31
|
33
|
|
|
32
|
34
|
ObjectMapper mapper = new ObjectMapper();
|
|
33
|
35
|
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
34
|
|
- mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
36
|
+ mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
|
35
|
37
|
serializer.setObjectMapper(mapper);
|
|
36
|
38
|
|
|
37
|
|
- template.setValueSerializer(serializer);
|
|
38
|
39
|
// 使用StringRedisSerializer来序列化和反序列化redis的key值
|
|
39
|
40
|
template.setKeySerializer(new StringRedisSerializer());
|
|
|
41
|
+ template.setValueSerializer(serializer);
|
|
|
42
|
+
|
|
|
43
|
+ // Hash的key也采用StringRedisSerializer的序列化方式
|
|
|
44
|
+ template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
45
|
+ template.setHashValueSerializer(serializer);
|
|
|
46
|
+
|
|
40
|
47
|
template.afterPropertiesSet();
|
|
41
|
48
|
return template;
|
|
42
|
49
|
}
|