在SpringBoot中,默认情况下使用的是Jackson作为JSON的序列化和反序列化库。但有时候,我们可能需要切换到其他的JSON库,比如Fastjson。Fastjson是阿里巴巴的一个开源项目,它提供了高性能的JSON序列化和反序列化功能。
Fastjson是一个Java库,可用于将Java对象转换为其JSON表示,它也可用于将JSON字符串转换为等效的Java对象。Fastjson可以处理任意Java对象,包括没有源代码的预先存在的对象。
温少在2010年3月加入阿里巴巴至今,Fastjson是监控系统实现的副产品,最后由阿里巴巴开发。
Fastjson可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到Java Bean。Fastjson采用独创的算法,将JSON Parse的速度提升到极致,超过所有其他JSON库,包括曾经号称最快的Jackson。
Fastjson 源码地址:https://github.com/alibaba/fastjson
Fastjson2源码地址:https://github.com/alibaba/fastjson2
Fastjson 中文 Wiki:https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
Jackson和Fastjson是两个常用的Java JSON处理库,在使用过程中的优缺点如下:
Jackson和Fastjson在SpringBoot项目中使用方面各有千秋。在实际使用中,可以根据项目的具体需求来选择合适的库。
系统:Macbook Pro 13.2
IDEA:IntelliJ IDEA 2022.3.2 (Ultimate Edition)
JDK:1.8+
我们为了演示效果,可以使用最快捷的方式创建一个SpringBoot项目。当然下面两个方式都是可以使用IDEA创建一个项目。由于IDEA版本差异,有些操作可能不同,请按照自己IDEA版本进行操作。
【创建一个空项目】
如果熟悉创建项目操作,我们也可以创建一个空项目。操作步骤如下:
我们在IDE中创建一个SpringBoot项目,项目名称为springboot-tool-fastjson
,并将项目放在一个合适的位置,例如1⃣️;在语言处,选择Java
,例如2⃣️;然后使用maven
创建项目,例如3⃣️;JDK环境选择自己电脑默认的即可,也可以按照需要进行选择,例如4⃣,我这里使用JDK8️;最后定位项目所在的分组以及项目所属项目,例如5⃣️。点击Create即可创建项目。
如果是使用Maven作为包管理仓库,则需要引入下面的最新版本的依赖包。截止目前为止,在v2版本中
最新版本为fastjson-2.0.45
。本文作者在操作时,引入此版本。
<!-- 引入fastjson依赖 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.45</version>
</dependency>
或者引入阿里巴巴fastjson2-2.0.45
。
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.45</version>
</dependency>
如果是使用Maven作为包管理仓库,则需要引入下面的最新版本的依赖包。截止目前为止,在v1版本
中最新版本为fastjson-1.2.83
。
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
在我们的pom.xml
文件中,默认情况下是使用的jackson
,当我们添加Fastjson
的依赖后,需要排除掉默认的Jackson
依赖(如果你不需要它的话)。
<!-- 引入SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
<!-- 如果你不需要Jackson,可以通过以下方式排除它 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
在修改完pom.xml
之后,我们需要在请求时处理数据,实现WebMvcConfigurer
配置,处理中文乱码等。
package org.example.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
/**
* WebConfig
*
* @author Aion.Liu
* @version v1.0.0
* @since 2024/1/28 22:39
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 1. 定义一个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2. 添加fastjson的配置信息
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat
);
fastConverter.setFastJsonConfig(fastJsonConfig);
// 3. 处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
// 4. 在converters中添加fastjson的配置信息
converters.add(0, fastConverter);
}
}
启动你的Spring Boot应用,并尝试发送一个HTTP请求。你应该会看到Fastjson被用来序列化和反序列化JSON数据。
下面是一个完整的pom.xml
引入功能。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 引入框架父类 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>springboot-tool-fastjson</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 引入fastjson依赖 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.45</version>
</dependency>
<!-- 引入SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.4.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
在使用Fastjson时,需要注意以下几点,以确保代码的正确性和性能。
作为一个Java开发工程师,应该熟悉使用fastjson。此外,还应该了解其他JSON工具,例如Gson、Jackson等。本文详细介绍在SpringBoot项目中如何使用Fastjson,以及去除默认Jackson工具的方法来使用fastjson,希望对其他开发者有帮助。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。