From 66b8b1e8b8ee77875e17772901248978a7f203dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Wed, 24 Sep 2025 17:08:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(jackson):=20=E4=BD=BF=E7=94=A8Jackson2Obje?= =?UTF-8?q?ctMapperBuilder=E9=85=8D=E7=BD=AEObjectMapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用Jackson2ObjectMapperBuilder构建ObjectMapper以确保与Spring Boot完全兼容 - 配置JavaTimeModule以支持Java 8时间类型 - 设置时区为GMT+8- 禁用将日期写为时间戳 - 忽略未知属性的反序列化错误 - 在application.yml中显式启用JavaTimeModule模块 --- .../common/core/config/JacksonConfig.java | 14 ++++++++++---- src/main/resources/application.yml | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java b/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java index e8ec164..c8fe5a7 100644 --- a/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java +++ b/src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java @@ -9,9 +9,11 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.TimeZone; /** * Jackson配置类 - 强制配置版本 @@ -29,12 +31,13 @@ public class JacksonConfig { private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** - * 强制配置ObjectMapper,确保JavaTimeModule被正确注册 + * 使用Jackson2ObjectMapperBuilder构建ObjectMapper + * 确保与Spring Boot完全兼容 */ @Bean @Primary - public ObjectMapper objectMapper() { - ObjectMapper mapper = new ObjectMapper(); + public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { + ObjectMapper mapper = builder.build(); // 创建并配置JavaTimeModule JavaTimeModule javaTimeModule = new JavaTimeModule(); @@ -49,9 +52,12 @@ public class JacksonConfig { // 禁用将日期写为时间戳 mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - + // 忽略未知属性 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + // 设置时区 + mapper.setTimeZone(TimeZone.getTimeZone("GMT+8")); return mapper; } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4888d93..751eff3 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -26,6 +26,9 @@ spring: write-dates-as-timestamps: false deserialization: fail-on-unknown-properties: false + # 确保启用Java 8时间支持 + modules: + - com.fasterxml.jackson.datatype.jsr310.JavaTimeModule # 连接池配置 datasource: