From cf3239e6bb351b995d4d3835aefd8c6064484e56 Mon Sep 17 00:00:00 2001 From: dengwc Date: Wed, 17 Apr 2024 15:01:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AB=98=E5=BE=B7=E5=AE=8C=E5=8D=95=E3=80=81?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E8=AE=A2=E5=8D=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/LocalDateTimeAdapter.java | 25 +++++++++++++++++++ .../interfaces/rest/base/PostRequest.java | 4 +++ 2 files changed, 29 insertions(+) create mode 100644 src/main/java/com/chint/infrastructure/util/LocalDateTimeAdapter.java diff --git a/src/main/java/com/chint/infrastructure/util/LocalDateTimeAdapter.java b/src/main/java/com/chint/infrastructure/util/LocalDateTimeAdapter.java new file mode 100644 index 00000000..2d9fbbeb --- /dev/null +++ b/src/main/java/com/chint/infrastructure/util/LocalDateTimeAdapter.java @@ -0,0 +1,25 @@ +package com.chint.infrastructure.util; + +import com.google.gson.*; +import java.lang.reflect.Type; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * GSON序列化LocalDateTime适配器 + */ +public class LocalDateTimeAdapter implements JsonSerializer, JsonDeserializer { + + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"); + + @Override + public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(formatter.format(src)); + } + + @Override + public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return LocalDateTime.parse(json.getAsString(), formatter); + } +} + diff --git a/src/main/java/com/chint/interfaces/rest/base/PostRequest.java b/src/main/java/com/chint/interfaces/rest/base/PostRequest.java index 5ad41d8a..dd321581 100644 --- a/src/main/java/com/chint/interfaces/rest/base/PostRequest.java +++ b/src/main/java/com/chint/interfaces/rest/base/PostRequest.java @@ -1,10 +1,12 @@ package com.chint.interfaces.rest.base; import com.alibaba.fastjson.JSON; +import com.chint.infrastructure.util.LocalDateTimeAdapter; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Charsets; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.http.Header; @@ -32,6 +34,7 @@ import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; import java.net.URLEncoder; +import java.time.LocalDateTime; import java.util.*; import static com.chint.infrastructure.constant.CommonMessageConstant.REDIRECT_NOT_EXIST; @@ -181,6 +184,7 @@ public class PostRequest { public T postANBPM(String url, Object jsonRequest, Class responseType) { HttpPost post = new HttpPost(url); + Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter()).create();//能够正常序列化对象的LocalDateTime字段 String json = gson.toJson(jsonRequest); log.info(json); post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));