Springboot 配置 FastJson 替换 Jackson

发布于:2025-06-22 ⋅ 阅读:(18) ⋅ 点赞:(0)

FastJson 常用 Api 文档

FastJson.md


FastJson介绍

FastJson 是阿里巴巴的开源JSON解析库,它可以解析 JSON 格式的字符串,支持将 Java Bean 序列化为 JSON 字符串,也可以从 JSON 字符串反序列化到 JavaBean。

Fastjson 的优点

  • 速度快
    • fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。
  • 使用广泛
    • fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受。在2012年被开源中国评选为最受欢迎的国产开源软件之一。
  • 测试完备
    • fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个。每次发布都会进行回归测试,保证质量稳定。
  • 使用简单
    • fastjson的 API 十分简洁。
  • 功能完备
    • 支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。

1. 排除 Jackson 依赖

Spring Boot 的 spring-boot-starter-web 默认集成 Jackson

需在 pom.xml(Maven 项目)或 build.gradle(Gradle 项目)中排除 Jackson 相关依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <!-- 排除 Jackson 依赖 -->
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
            </exclusion>
        </exclusions>
</dependency>

2. 添加 Fastjson 依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.53</version> <!-- 版本可按需调整 -->
</dependency>

3. 配置 Fastjson 消息转换器

创建一个配置类,实现 WebMvcConfigurer 接口

重写 configureMessageConverters 方法来注册 Fastjson 的消息转换器

package org.example.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Configuration
public class WebMvcFastjsonConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 1. 创建 Fastjson 消息转换器  
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();

        // 2. 配置 Fastjson 序列化规则(按需设置)  
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 示例:设置日期格式、输出空值字段、格式化 JSON 等
        // 其他序列化特性(按需添加),比如:
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setSerializerFeatures(
                com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue, // 输出空值字段
                com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat // 格式化输出
        );

        // 3. 设置编码(避免中文乱码)
        fastJsonConverter.setDefaultCharset(StandardCharsets.UTF_8);
        fastJsonConverter.setFastJsonConfig(fastJsonConfig);

        // 4. 将 Fastjson 转换器加入列表,可调整顺序(如放首位覆盖默认 Jackson)  
        converters.add(0, fastJsonConverter);
    }
}  

4. 测试验证

启动 Spring Boot 应用,访问 http://localhost:8080/test

若返回的 JSON 符合 Fastjson 的序列化规则(如日期格式、空值处理等按配置的 Fastjson 规则),则说明替换成功。

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {

    @GetMapping("/test")
    public Map<String, Object> test() {
        Map<String, Object> result = new HashMap<>();
        result.put("name", "张三");
        result.put("age", 20);
        return result;
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到