From 209769d0245afc63f3e5912f994cf81d1378ec18 Mon Sep 17 00:00:00 2001 From: zkh <1650697374@qq.com> Date: Wed, 31 Dec 2025 12:13:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(jackson):=20=E6=B7=BB=E5=8A=A0=20LocalDate?= =?UTF-8?q?Time=20=E5=92=8C=20LocalDate=20=E7=9A=84=20JSON=20=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入 LocalDateTimeSerializer 和 LocalDateTimeDeserializer 处理 LocalDateTime 类型 - 添加 LocalDateSerializer 和 LocalDateDeserializer 处理 LocalDate 类型 - 配置 Jackson 使用自定义的时间格式化器 - 扩展 ObjectMapper 配置以支持新的序列化器和反序列化器 - 实现时间类型的空值安全处理 - 统一时间格式为 "yyyy-MM-dd HH:mm:ss" 和 "yyyy-MM-dd" --- .../vip/jcfd/web/config/JacksonConfig.java | 81 ++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/zkh-web/src/main/java/vip/jcfd/web/config/JacksonConfig.java b/zkh-web/src/main/java/vip/jcfd/web/config/JacksonConfig.java index d39fff7..7831c05 100644 --- a/zkh-web/src/main/java/vip/jcfd/web/config/JacksonConfig.java +++ b/zkh-web/src/main/java/vip/jcfd/web/config/JacksonConfig.java @@ -11,7 +11,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Locale; import java.util.TimeZone; @@ -28,11 +31,85 @@ public class JacksonConfig { jacksonObjectMapperBuilder.locale(Locale.CHINA); jacksonObjectMapperBuilder.timeZone(TimeZone.getTimeZone(ZoneId.of("Asia/Shanghai"))); jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd HH:mm:ss"); - jacksonObjectMapperBuilder.serializers(new LongSerializer()); - jacksonObjectMapperBuilder.deserializers(new LongDeserializer()); + jacksonObjectMapperBuilder.serializers(new LongSerializer(), new LocalDateTimeSerializer(), new LocalDateSerializer()); + jacksonObjectMapperBuilder.deserializers(new LongDeserializer(), new LocalDateTimeDeserializer(), new LocalDateDeserializer()); }; } + public static class LocalDateTimeSerializer extends JsonSerializer { + + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + @Override + public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + if (value == null) { + gen.writeNull(); + return; + } + gen.writeString(value.format(formatter)); + } + + @Override + public Class handledType() { + return LocalDateTime.class; + } + } + + public static class LocalDateTimeDeserializer extends JsonDeserializer { + + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + @Override + public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + if (p.getText() == null) { + return null; + } + return LocalDateTime.parse(p.getText(), formatter); + } + + @Override + public Class handledType() { + return LocalDateTime.class; + } + } + + public static class LocalDateSerializer extends JsonSerializer { + + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + @Override + public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + if (value == null) { + gen.writeNull(); + return; + } + gen.writeString(value.format(formatter)); + } + + @Override + public Class handledType() { + return LocalDate.class; + } + } + + public static class LocalDateDeserializer extends JsonDeserializer { + + private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + @Override + public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + if (p.getText() == null) { + return null; + } + return LocalDate.parse(p.getText(), formatter); + } + + @Override + public Class handledType() { + return LocalDate.class; + } + } + public static class LongSerializer extends JsonSerializer { @Override public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {