From e2473f463021c787bfc9ec4b751a293bd5b5ceba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=AD=E5=87=AF=E5=87=AF?= Date: Wed, 12 Feb 2025 09:50:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4fastjosn1.x=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96fastjson2=E9=85=8D=E7=BD=AE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/czg/config/FastJson2Config.java | 41 +++++++++++-------- cash-common/cash-common-tools/pom.xml | 6 +++ cash-dependencies/pom.xml | 5 +++ cash-service/account-service/pom.xml | 6 +++ cash-service/pom.xml | 6 +++ 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/cash-common/cash-common-api-config/src/main/java/com/czg/config/FastJson2Config.java b/cash-common/cash-common-api-config/src/main/java/com/czg/config/FastJson2Config.java index a7a23ab82..a4c28e015 100644 --- a/cash-common/cash-common-api-config/src/main/java/com/czg/config/FastJson2Config.java +++ b/cash-common/cash-common-api-config/src/main/java/com/czg/config/FastJson2Config.java @@ -1,8 +1,10 @@ package com.czg.config; -import com.alibaba.fastjson.serializer.SerializerFeature; -import com.alibaba.fastjson.support.config.FastJsonConfig; -import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; + +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.support.config.FastJsonConfig; +import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; @@ -25,24 +27,31 @@ public class FastJson2Config implements WebMvcConfigurer { // 创建 FastJson 消息转换器 FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); // 创建 FastJson 配置 - FastJsonConfig fastJsonConfig = new FastJsonConfig(); + FastJsonConfig config = new FastJsonConfig(); // 设置日期格式 - fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); - // 设置序列化特性 - // 例如,将空集合序列化为 [],空字符串序列化为 "" - fastJsonConfig.setSerializerFeatures( - SerializerFeature.WriteNullListAsEmpty, - SerializerFeature.WriteNullStringAsEmpty + config.setDateFormat("yyyy-MM-dd HH:mm:ss"); + + // 设置反序列化特性 + config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean); + config.setWriterFeatures( + JSONWriter.Feature.WriteMapNullValue, + // 格式化输出 + JSONWriter.Feature.PrettyFormat, + // Long类型序列化为字符串 + JSONWriter.Feature.WriteLongAsString, + // String类型值为空序列化为"" + JSONWriter.Feature.WriteNullStringAsEmpty ); + // 将配置设置到转换器 - converter.setFastJsonConfig(fastJsonConfig); - // 设置支持的媒体类型 - List mediaTypes = new ArrayList<>(); - mediaTypes.add(MediaType.APPLICATION_JSON); - mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); - converter.setSupportedMediaTypes(mediaTypes); + converter.setFastJsonConfig(config); // 设置字符编码 converter.setDefaultCharset(StandardCharsets.UTF_8); + // 设置支持的媒体类型 + List supportedMediaTypes = new ArrayList<>(); + supportedMediaTypes.add(MediaType.APPLICATION_JSON); + supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); + converter.setSupportedMediaTypes(supportedMediaTypes); // 将转换器添加到 Spring MVC 的消息转换器列表中 converters.add(0, converter); } diff --git a/cash-common/cash-common-tools/pom.xml b/cash-common/cash-common-tools/pom.xml index 8a12d0bc1..75afc7f95 100644 --- a/cash-common/cash-common-tools/pom.xml +++ b/cash-common/cash-common-tools/pom.xml @@ -16,6 +16,12 @@ com.alipay.sdk alipay-sdk-java + + + fastjson + com.alibaba + + cn.hutool diff --git a/cash-dependencies/pom.xml b/cash-dependencies/pom.xml index d0742ff07..02af4b3df 100644 --- a/cash-dependencies/pom.xml +++ b/cash-dependencies/pom.xml @@ -146,6 +146,11 @@ fastjson2 ${fastjson2.version} + + com.alibaba.fastjson2 + fastjson2-extension-spring6 + ${fastjson2.version} + diff --git a/cash-service/account-service/pom.xml b/cash-service/account-service/pom.xml index 3aa2b655f..356fd59aa 100644 --- a/cash-service/account-service/pom.xml +++ b/cash-service/account-service/pom.xml @@ -30,6 +30,12 @@ com.alipay.sdk alipay-sdk-java + + + fastjson + com.alibaba + + diff --git a/cash-service/pom.xml b/cash-service/pom.xml index c1a50da8f..f132f2bbb 100644 --- a/cash-service/pom.xml +++ b/cash-service/pom.xml @@ -33,6 +33,12 @@ com.czg cash-common-tools ${project.version} + + + fastjson + com.alibaba + +