移除fastjosn1.x依赖,优化fastjson2配置类

This commit is contained in:
谭凯凯
2025-02-12 09:50:38 +08:00
committed by Tankaikai
parent 9b25a2b09b
commit e2473f4630
5 changed files with 48 additions and 16 deletions

View File

@@ -1,8 +1,10 @@
package com.czg.config; package com.czg.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 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.context.annotation.Configuration;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
@@ -25,24 +27,31 @@ public class FastJson2Config implements WebMvcConfigurer {
// 创建 FastJson 消息转换器 // 创建 FastJson 消息转换器
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
// 创建 FastJson 配置 // 创建 FastJson 配置
FastJsonConfig fastJsonConfig = new FastJsonConfig(); FastJsonConfig config = new FastJsonConfig();
// 设置日期格式 // 设置日期格式
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); config.setDateFormat("yyyy-MM-dd HH:mm:ss");
// 设置序列化特性
// 例如,将空集合序列化为 [],空字符串序列化为 "" // 设置反序列化特性
fastJsonConfig.setSerializerFeatures( config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
SerializerFeature.WriteNullListAsEmpty, config.setWriterFeatures(
SerializerFeature.WriteNullStringAsEmpty JSONWriter.Feature.WriteMapNullValue,
// 格式化输出
JSONWriter.Feature.PrettyFormat,
// Long类型序列化为字符串
JSONWriter.Feature.WriteLongAsString,
// String类型值为空序列化为""
JSONWriter.Feature.WriteNullStringAsEmpty
); );
// 将配置设置到转换器 // 将配置设置到转换器
converter.setFastJsonConfig(fastJsonConfig); converter.setFastJsonConfig(config);
// 设置支持的媒体类型
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON);
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(mediaTypes);
// 设置字符编码 // 设置字符编码
converter.setDefaultCharset(StandardCharsets.UTF_8); converter.setDefaultCharset(StandardCharsets.UTF_8);
// 设置支持的媒体类型
List<MediaType> supportedMediaTypes = new ArrayList<>();
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(supportedMediaTypes);
// 将转换器添加到 Spring MVC 的消息转换器列表中 // 将转换器添加到 Spring MVC 的消息转换器列表中
converters.add(0, converter); converters.add(0, converter);
} }

View File

@@ -16,6 +16,12 @@
<dependency> <dependency>
<groupId>com.alipay.sdk</groupId> <groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId> <artifactId>alipay-sdk-java</artifactId>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>

View File

@@ -146,6 +146,11 @@
<artifactId>fastjson2</artifactId> <artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version> <version>${fastjson2.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-spring6</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<!-- Spring Boot与nacos整合的核心依赖 --> <!-- Spring Boot与nacos整合的核心依赖 -->
<dependency> <dependency>

View File

@@ -30,6 +30,12 @@
<dependency> <dependency>
<groupId>com.alipay.sdk</groupId> <groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId> <artifactId>alipay-sdk-java</artifactId>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -33,6 +33,12 @@
<groupId>com.czg</groupId> <groupId>com.czg</groupId>
<artifactId>cash-common-tools</artifactId> <artifactId>cash-common-tools</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>