From 321f71de73fd91dbaf6f334e12260e3feefd6300 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 10 Apr 2024 13:03:20 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=20LocalDateTimeDeserializer?= =?UTF-8?q?=20=E5=92=8C=20LocalDateTimeSerializer=20=E7=9A=84=E5=91=BD?= =?UTF-8?q?=E5=90=8D=EF=BC=8C=E5=8F=AF=E8=AF=BB=E6=80=A7=E6=9B=B4=E5=A5=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/YudaoJacksonAutoConfiguration.java | 12 ++++++------ ....java => TimestampLocalDateTimeDeserializer.java} | 12 +++++++----- ...er.java => TimestampLocalDateTimeSerializer.java} | 12 +++++++----- 3 files changed, 20 insertions(+), 16 deletions(-) rename yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/{LocalDateTimeDeserializer.java => TimestampLocalDateTimeDeserializer.java} (62%) rename yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/{LocalDateTimeSerializer.java => TimestampLocalDateTimeSerializer.java} (62%) diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/config/YudaoJacksonAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/config/YudaoJacksonAutoConfiguration.java index 6ea95b196..4f94f16ec 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/config/YudaoJacksonAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/config/YudaoJacksonAutoConfiguration.java @@ -2,9 +2,9 @@ package cn.iocoder.yudao.framework.jackson.config; import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; -import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer; -import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer; import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer; +import cn.iocoder.yudao.framework.jackson.core.databind.TimestampLocalDateTimeDeserializer; +import cn.iocoder.yudao.framework.jackson.core.databind.TimestampLocalDateTimeSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; @@ -37,13 +37,13 @@ public class YudaoJacksonAutoConfiguration { .addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE) .addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE) .addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE) - // 新增 LocalDateTime 序列化、反序列化规则 - .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) - .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); + // 新增 LocalDateTime 序列化、反序列化规则,使用 Long 时间戳 + .addSerializer(LocalDateTime.class, TimestampLocalDateTimeSerializer.INSTANCE) + .addDeserializer(LocalDateTime.class, TimestampLocalDateTimeDeserializer.INSTANCE); // 1.2 注册到 objectMapper objectMappers.forEach(objectMapper -> objectMapper.registerModule(simpleModule)); - // 2. 设置 objectMapper 到 JsonUtils { + // 2. 设置 objectMapper 到 JsonUtils JsonUtils.init(CollUtil.getFirst(objectMappers)); log.info("[init][初始化 JsonUtils 成功]"); return new JsonUtils(); diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeDeserializer.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeDeserializer.java similarity index 62% rename from yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeDeserializer.java rename to yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeDeserializer.java index 53c40254b..71a480fbf 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeDeserializer.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeDeserializer.java @@ -10,16 +10,18 @@ import java.time.LocalDateTime; import java.time.ZoneId; /** - * LocalDateTime反序列化规则 - *

- * 会将毫秒级时间戳反序列化为LocalDateTime + * 基于时间戳的 LocalDateTime 反序列化器 + * + * @author 老五 */ -public class LocalDateTimeDeserializer extends JsonDeserializer { +public class TimestampLocalDateTimeDeserializer extends JsonDeserializer { - public static final LocalDateTimeDeserializer INSTANCE = new LocalDateTimeDeserializer(); + public static final TimestampLocalDateTimeDeserializer INSTANCE = new TimestampLocalDateTimeDeserializer(); @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + // 将 Long 时间戳,转换为 LocalDateTime 对象 return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getValueAsLong()), ZoneId.systemDefault()); } + } diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeSerializer.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeSerializer.java similarity index 62% rename from yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeSerializer.java rename to yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeSerializer.java index 286fb733e..e72c47bb8 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/LocalDateTimeSerializer.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/core/databind/TimestampLocalDateTimeSerializer.java @@ -9,16 +9,18 @@ import java.time.LocalDateTime; import java.time.ZoneId; /** - * LocalDateTime序列化规则 - *

- * 会将LocalDateTime序列化为毫秒级时间戳 + * 基于时间戳的 LocalDateTime 序列化器 + * + * @author 老五 */ -public class LocalDateTimeSerializer extends JsonSerializer { +public class TimestampLocalDateTimeSerializer extends JsonSerializer { - public static final LocalDateTimeSerializer INSTANCE = new LocalDateTimeSerializer(); + public static final TimestampLocalDateTimeSerializer INSTANCE = new TimestampLocalDateTimeSerializer(); @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + // 将 LocalDateTime 对象,转换为 Long 时间戳 gen.writeNumber(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); } + }