Просмотр исходного кода

:zap: 优化性能:优化jdk1.8下jackson的序列化表现

暖暖 6 лет назад
Родитель
Сommit
efb81270f2

+ 9 - 4
pigx-common/pigx-common-core/pom.xml

@@ -70,12 +70,17 @@
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-validation</artifactId>
 		</dependency>
-		<!--jacksonJSR310规范-->
 		<dependency>
 			<groupId>com.fasterxml.jackson.module</groupId>
-			<artifactId>jackson-modules-java8</artifactId>
-			<version>${jackson.modules}</version>
-			<type>pom</type>
+			<artifactId>jackson-module-parameter-names</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.fasterxml.jackson.datatype</groupId>
+			<artifactId>jackson-datatype-jdk8</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>com.fasterxml.jackson.datatype</groupId>
+			<artifactId>jackson-datatype-jsr310</artifactId>
 		</dependency>
 	</dependencies>
 </project>

+ 7 - 4
pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/config/JacksonConfig.java

@@ -22,6 +22,9 @@ package com.pig4cloud.pigx.common.core.config;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
 import org.springframework.boot.autoconfigure.AutoConfigureBefore;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
@@ -41,16 +44,16 @@ import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
 public class JacksonConfig {
 	/**
 	 * 针对JDK 1.8 特殊处理
-	 * .registerModule(new ParameterNamesModule())
-	 * .registerModule(new Jdk8Module())
-	 * .registerModule(new JavaTimeModule()); // new module, NOT JSR310Module
 	 *
 	 * @return
 	 */
 	@Bean
 	public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder) {
 		ObjectMapper objectMapper = builder.createXmlMapper(false).build();
-		objectMapper.findAndRegisterModules();
+		ObjectMapper mapper = new ObjectMapper()
+			.registerModule(new ParameterNamesModule())
+			.registerModule(new Jdk8Module())
+			.registerModule(new JavaTimeModule()); // new module, NOT JSR310Module
 		// 忽略json字符串中不识别的属性
 		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 		// 忽略无法转换的对象

+ 4 - 0
pigx-config/src/main/resources/config/application-dev.yml

@@ -7,6 +7,10 @@ spring:
   redis:
     password:
     host: redis
+  # Jackson配置
+  jackson:
+    time-zone: GMT+8
+    date-format: yyyy-MM-dd HH:mm:ss
 # 暴露监控端点
 management:
   endpoints:

+ 0 - 3
pigx-config/src/main/resources/config/pigx-codegen-dev.yml

@@ -14,8 +14,5 @@ spring:
     username: root
     password: 123456
     url: jdbc:mysql://mysql:3306/pigx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
-  jackson:
-    time-zone: GMT+8
-    date-format: yyyy-MM-dd HH:mm:ss
   resources:
     static-locations: classpath:/static/,classpath:/views/

+ 8 - 0
pom.xml

@@ -134,6 +134,14 @@
 				<type>pom</type>
 				<scope>import</scope>
 			</dependency>
+			<!--jacksonJSR310规范-->
+			<dependency>
+				<groupId>com.fasterxml.jackson.module</groupId>
+				<artifactId>jackson-modules-java8</artifactId>
+				<version>${jackson.modules}</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
 		</dependencies>
 	</dependencyManagement>