From 8046974400cbf18b7dc030a02ec302990989b336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Sat, 8 Sep 2018 22:12:07 +0800 Subject: [PATCH] =?UTF-8?q?:arrow=5Fup:=20=E4=BE=9D=E8=B5=96=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=EF=BC=8C=20=E5=8D=87=E7=BA=A7Jackson.modules=20JDK8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-common/pigx-common-core/pom.xml | 6 ++- .../common/core/config/JacksonConfig.java | 40 ++++++++----------- pom.xml | 1 + 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/pigx-common/pigx-common-core/pom.xml b/pigx-common/pigx-common-core/pom.xml index 6074f0c68..a74dc9f6d 100644 --- a/pigx-common/pigx-common-core/pom.xml +++ b/pigx-common/pigx-common-core/pom.xml @@ -72,8 +72,10 @@ - com.fasterxml.jackson.datatype - jackson-datatype-jsr310 + com.fasterxml.jackson.module + jackson-modules-java8 + ${jackson.modules} + pom diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java index 37792eda6..f828857dd 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java @@ -21,20 +21,13 @@ package com.pig4cloud.pigx.common.core.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; -import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; -import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; - -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; /** * JacksonConfig @@ -43,24 +36,25 @@ import java.time.format.DateTimeFormatter; * @date: 2018/7/28 12:29 */ @Configuration +@ConditionalOnClass(ObjectMapper.class) +@AutoConfigureBefore(JacksonAutoConfiguration.class) public class JacksonConfig { /** - * 处理序列化后的1.8的日期时间格式 + * 针对JDK 1.8 特殊处理 + * .registerModule(new ParameterNamesModule()) + * .registerModule(new Jdk8Module()) + * .registerModule(new JavaTimeModule()); // new module, NOT JSR310Module * * @return */ @Bean - public ObjectMapper getObjectMapper() { - ObjectMapper objectMapper = new ObjectMapper(); - JavaTimeModule javaTimeModule = new JavaTimeModule(); - javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); - javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); - javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); - javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); - javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); - javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); - objectMapper.registerModule(javaTimeModule); + public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder) { + ObjectMapper objectMapper = builder.createXmlMapper(false).build(); + objectMapper.findAndRegisterModules(); + // 忽略json字符串中不识别的属性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + // 忽略无法转换的对象 + objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); return objectMapper; } } diff --git a/pom.xml b/pom.xml index c1b23fc7f..d478fe154 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,7 @@ 2.1.0 2.1.5 2.3.3.RELEASE + 2.9.6 http://192.168.0.13:4243 192.168.0.13:5000