refactor(redis): 适配 Spring Data Redis 4.0 序列化器变更

Jackson2JsonRedisSerializer 在 Spring Data Redis 4.0 中已标记为弃用并计划移除。
替换为 JacksonJsonRedisSerializer,对应 Jackson 3 的 API 变更:
This commit is contained in:
Ray.Hao
2026-05-20 14:07:36 +08:00
parent 481152ee1f
commit 65333b9e8e

View File

@@ -1,14 +1,13 @@
package com.youlai.boot.framework.cache; package com.youlai.boot.framework.cache;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.json.JsonMapper;
/** /**
* Redis 配置 * Redis 配置
@@ -38,13 +37,11 @@ public class RedisConfig {
redisTemplate.setHashKeySerializer(RedisSerializer.string()); redisTemplate.setHashKeySerializer(RedisSerializer.string());
// Value 使用自定义 JSON 序列化(不写入类型信息,避免 HashSet 等集合被序列化成带 @class 的结构) // Value 使用自定义 JSON 序列化(不写入类型信息,避免 HashSet 等集合被序列化成带 @class 的结构)
ObjectMapper objectMapper = new ObjectMapper(); JsonMapper jsonMapper = JsonMapper.builder()
objectMapper.registerModule(new JavaTimeModule()); .disable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS)
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); .build();
// 禁用类型信息写入,避免集合类型名被当成元素
objectMapper.disableDefaultTyping();
Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class); JacksonJsonRedisSerializer<Object> jsonSerializer = new JacksonJsonRedisSerializer<>(jsonMapper, Object.class);
redisTemplate.setValueSerializer(jsonSerializer); redisTemplate.setValueSerializer(jsonSerializer);
redisTemplate.setHashValueSerializer(jsonSerializer); redisTemplate.setHashValueSerializer(jsonSerializer);