移除fastjosn1.x依赖,优化fastjson2配置类
This commit is contained in:
@@ -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<MediaType> 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<MediaType> supportedMediaTypes = new ArrayList<>();
|
||||
supportedMediaTypes.add(MediaType.APPLICATION_JSON);
|
||||
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
|
||||
converter.setSupportedMediaTypes(supportedMediaTypes);
|
||||
// 将转换器添加到 Spring MVC 的消息转换器列表中
|
||||
converters.add(0, converter);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,12 @@
|
||||
<dependency>
|
||||
<groupId>com.alipay.sdk</groupId>
|
||||
<artifactId>alipay-sdk-java</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<groupId>com.alibaba</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
|
||||
@@ -146,6 +146,11 @@
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>${fastjson2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2-extension-spring6</artifactId>
|
||||
<version>${fastjson2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot与nacos整合的核心依赖 -->
|
||||
<dependency>
|
||||
|
||||
@@ -30,6 +30,12 @@
|
||||
<dependency>
|
||||
<groupId>com.alipay.sdk</groupId>
|
||||
<artifactId>alipay-sdk-java</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<groupId>com.alibaba</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
@@ -33,6 +33,12 @@
|
||||
<groupId>com.czg</groupId>
|
||||
<artifactId>cash-common-tools</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<groupId>com.alibaba</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
Reference in New Issue
Block a user