当前位置: 首页 > news >正文

Springboot 配置 FastJson 替换 Jackson

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 {@Overridepublic 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;}
}

http://www.lryc.cn/news/573391.html

相关文章:

  • Rabbitmq集成springboot,手动确认消息basicAck、basicNack、basicReject的使用
  • 在 MyBatis 的xml中,什么时候大于号和小于号可以不用转义
  • Axios 在 Vue3 项目中的使用:从安装到组件中的使用
  • 升级到 .NET 9 分步指南
  • “最浅”的陷阱:聊聊二叉树最小深度的递归坑点与解法哲学
  • 秋招Day14 - MySQL - SQL优化
  • c++11标准(5)——并发库(互斥锁)
  • 一、什么是生成式人工智能
  • 终端里的AI黑魔法:OpenCode深度体验与架构揭秘
  • Java ArrayList集合和HashSet集合详解
  • 【论文笔记】【强化微调】TinyLLaVA-Video-R1:小参数模型也能视频推理
  • 人人都是音乐家?腾讯开源音乐生成大模型SongGeneration
  • 旧物回收小程序开发:开启绿色生活新方式
  • Python列表常用操作方法
  • 从语义到推荐:大语言模型(LLM)如何驱动智能选车系统?
  • 首页实现多级缓存
  • AWS-SAA 第二部份:安全性和权限管理
  • 《map和set的使用介绍》
  • Linux TCP/IP协议栈中的TCP输入处理:net/ipv4/tcp_input.c解析
  • TCP 三次握手与四次挥手全流程详解
  • 【智能体】n8n聊天获取链接后爬虫知乎
  • 设计模式精讲 Day 9:装饰器模式(Decorator Pattern)
  • 【RTP】基于mediasoup的RtpPacket的H.264打包、解包和demo 1:不含扩展
  • 2D曲线点云平滑去噪
  • 雨声_锦程_时年
  • linux生产环境下根据关键字搜索指定日志文件命令
  • 软件工程期末试卷选择题版带答案(共214道)
  • 借助ChatGPT快速开发图片转PDF的Python工具
  • Java大厂面试攻略:Spring Boot与微服务架构深度剖析
  • `shallowReactive` 与 `shallowRef`:浅层响应式 API