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));