|
1 | 1 | package top.cadecode.uniboot.common.plugin.cache.config; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.annotation.JsonTypeInfo.As; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; |
3 | 6 | import lombok.RequiredArgsConstructor; |
4 | 7 | import org.springframework.context.annotation.Bean; |
5 | 8 | import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.context.annotation.Primary; |
6 | 10 | import org.springframework.data.redis.connection.RedisConnectionFactory; |
7 | 11 | import org.springframework.data.redis.core.RedisTemplate; |
8 | 12 | import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
| 13 | +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; |
9 | 14 | import org.springframework.data.redis.serializer.RedisSerializer; |
| 15 | +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; |
10 | 16 |
|
11 | 17 | /** |
12 | 18 | * Redis 配置类 |
|
18 | 24 | @Configuration |
19 | 25 | public class RedisConfig { |
20 | 26 |
|
21 | | - private final RedisConnectionFactory factory; |
22 | | - |
23 | 27 | @Bean |
24 | | - RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { |
| 28 | + RedisMessageListenerContainer container(RedisConnectionFactory factory) { |
25 | 29 | RedisMessageListenerContainer container = new RedisMessageListenerContainer(); |
26 | | - container.setConnectionFactory(connectionFactory); |
| 30 | + container.setConnectionFactory(factory); |
27 | 31 | return container; |
28 | 32 | } |
29 | 33 |
|
| 34 | + /** |
| 35 | + * 全局默认 RedisTemplate |
| 36 | + * key string, val json |
| 37 | + */ |
| 38 | + @Primary |
30 | 39 | @Bean |
31 | | - public RedisTemplate<String, ?> jsonRedisTemplate() { |
32 | | - RedisTemplate<String, ?> template = new RedisTemplate<>(); |
| 40 | + public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory, Jackson2ObjectMapperBuilder builder) { |
| 41 | + // 创建 ObjectMapper,并复用 Spring 项目配置 |
| 42 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 43 | + builder.configure(objectMapper); |
| 44 | + // 启用序列化类型 |
| 45 | + objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), DefaultTyping.NON_FINAL, As.PROPERTY); |
| 46 | + // 创建 Redis Jackson 序列化器 |
| 47 | + GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(objectMapper); |
| 48 | + RedisTemplate<String, Object> template = new RedisTemplate<>(); |
33 | 49 | template.setConnectionFactory(factory); |
34 | 50 | // 设置 k v 的序列化方式 |
35 | 51 | template.setKeySerializer(RedisSerializer.string()); |
36 | | - template.setValueSerializer(RedisSerializer.json()); |
| 52 | + template.setValueSerializer(jsonRedisSerializer); |
37 | 53 | // 设置 hash k v 的序列化方式 |
38 | 54 | template.setHashKeySerializer(RedisSerializer.string()); |
39 | | - template.setHashValueSerializer(RedisSerializer.json()); |
| 55 | + template.setHashValueSerializer(jsonRedisSerializer); |
40 | 56 | return template; |
41 | 57 | } |
42 | 58 | } |
0 commit comments