diff --git a/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/SerializerConfiguration.java b/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/SerializerConfiguration.java index 12c76361..d61e1666 100644 --- a/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/SerializerConfiguration.java +++ b/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/SerializerConfiguration.java @@ -1,11 +1,14 @@ package in.hocg.boot.web.autoconfiguration.jackson; +import in.hocg.boot.web.autoconfiguration.jackson.localdatetime.LocalDateTimeDeserializer; import in.hocg.boot.web.autoconfiguration.jackson.xlong.LongDeserializer; import in.hocg.boot.web.autoconfiguration.jackson.xlong.LongSerializer; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.time.LocalDateTime; + /** * Created by hocgin on 2020/9/4 * email: hocgin@gmail.com @@ -19,7 +22,7 @@ public class SerializerConfiguration { public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { return builder -> { // builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer()); -// builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer()); + builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer()); builder.serializerByType(Long.class, new LongSerializer()); builder.deserializerByType(Long.class, new LongDeserializer()); }; diff --git a/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/localdatetime/LocalDateTimeDeserializer.java b/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/localdatetime/LocalDateTimeDeserializer.java index 687c22ea..c7a3bfe4 100644 --- a/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/localdatetime/LocalDateTimeDeserializer.java +++ b/spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/localdatetime/LocalDateTimeDeserializer.java @@ -1,5 +1,7 @@ package in.hocg.boot.web.autoconfiguration.jackson.localdatetime; +import cn.hutool.core.convert.Convert; +import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; @@ -21,7 +23,11 @@ public class LocalDateTimeDeserializer extends JsonDeserializer { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { if (Objects.nonNull(p) && StrUtil.isNotBlank(p.getText())) { - return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getLongValue()), ZoneOffset.of("+8")); + String text = p.getText(); + if (NumberUtil.isLong(text)) { + return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getLongValue()), ZoneOffset.of("+8")); + } + return Convert.toLocalDateTime(text); } return null; }