news 2026/4/16 19:52:15

SpringBoot3整合FastJSON2如何配置configureMessageConverters

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot3整合FastJSON2如何配置configureMessageConverters

在 Spring Boot 3 中整合 FastJSON 2 主要涉及到以下几个步骤,包括添加依赖、配置 FastJSON 作为 JSON 处理器等。下面是详细的步骤:

1. 添加依赖

首先,你需要在你的pom.xml文件中添加 FastJSON 2 的依赖。以下是 Maven 依赖的示例:

<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2-extension-spring6 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2-extension-spring6</artifactId> <version>2.0.53</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.53</version> </dependency>
2. 配置 FastJSON 作为 JSON 处理器

在 Spring Boot 3 中,默认的 JSON 处理器是 Jackson。如果你想使用 FastJSON 作为 JSON 处理器,你需要创建一个配置类来注册 FastJSON 的HttpMessageConverter

import com.alibaba.fastjson2.support.config.FastJsonConfig; import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter; import lombok.extern.slf4j.Slf4j; 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.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; @Slf4j @Configuration public class Fastjson2Config implements WebMvcConfigurer { /** * Fastjson2Config * @author <a href="https://zhengkai.blog.csdn.net/">zhengkai.blog.csdn.net</a> */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd HH:mm:ss"); // 其他配置... converter.setFastJsonConfig(config); converter.setDefaultCharset(StandardCharsets.UTF_8); converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON)); converters.add(0, converter); log.info("Fastjson2 Initial Done"); } }
3. 使用 FastJSON2 进行 JSON 序列化和反序列化

在你的 Spring Boot 应用中,你可以直接使用 FastJSON 的 API 进行 JSON 的序列化和反序列化。例如:

import com.alibaba.fastjson2.JSON; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class SampleController { @GetMapping("/json") public String getJson() { MyObject obj = new MyObject(); obj.setName("Test"); obj.setValue(123); // 使用 FastJSON 进行序列化 return JSON.toJSONString(obj); } public static class MyObject { private String name; private int value; // Getter 和 Setter public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } }
4. 测试

启动你的 Spring Boot 应用,并访问/json路径,你应该能够看到 FastJSON 生成的 JSON 响应。

开源项目

SpringBoot3脚手架,基于SpringBoot3+Druid+PgSQL+MyBatisPlus13+FastJSON2+Lombok,启动web容器为Undertow(非默认tomcat),其他的请自行添加和配置。

https://gitee.com/moshowgame/MySpringBootAPIhttps://github.com/moshowgame/MySpringBootAPI

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 14:32:41

SpringBoot 项目如何使用 pageHelper 做分页处理 (含两种依赖方式)

分页是常见大型项目都需要的一个功能&#xff0c;PageHelper是一个非常流行的MyBatis分页插件&#xff0c;它支持多数据库分页&#xff0c;无需修改SQL语句即可实现分页功能。 本文在最后展示了两种依赖验证的结果。 文章目录 一、第一种依赖方式二、第二种依赖方式三、创建数…

作者头像 李华
网站建设 2026/4/16 16:25:54

发布-订阅模式:流处理架构中的瑞士军刀

你好&#xff0c;我是程序员贵哥。 今天我想要与你分享的是在处理大规模数据中十分流行的一种设计模式&#xff1a;发布/订阅模式&#xff08;Publish/Subscribe Pattern&#xff09;&#xff0c;有些地方也称它为Pub/Sub。 在了解发布/订阅模式之前&#xff0c;我想先简单介…

作者头像 李华
网站建设 2026/4/16 3:59:19

Keil5乱码问题排查:编码一致性核心要点

Keil5中文注释乱码&#xff1f;一文讲透编码根源与实战解决你有没有遇到过这样的场景&#xff1a;在Keil5里打开一个C文件&#xff0c;明明昨天还清清楚楚的“初始化GPIO引脚”&#xff0c;今天却变成了“鍒濆鍖朞PIO寮曡剼”&#xff1f;或者更离谱地显示成一堆方框、问号&a…

作者头像 李华
网站建设 2026/4/16 14:29:31

利用HAL库实现浮点数据转换示例

从ADC采样到真实世界&#xff1a;用HAL库搞定浮点转换的那些事 你有没有遇到过这样的场景&#xff1f; 接上一个温度传感器&#xff0c;读出来的数值明明是12位ADC原始值&#xff08;比如 3056 &#xff09;&#xff0c;但你想知道的是“现在室温到底是23.7℃还是24.1℃”。…

作者头像 李华
网站建设 2026/4/16 12:51:54

AI智能体进化:学习与MCP协议实战

智能体的“自我修炼”与“通用接口”&#xff1a;学习适应与MCP协议实战解析 在智能体从“被动执行”走向“主动智能”的进化中&#xff0c;“学习与适应”是其突破预设局限的核心能力&#xff0c;而“模型上下文协议&#xff08;MCP&#xff09;”则是其打通外部世界的关键桥梁…

作者头像 李华
网站建设 2026/4/15 21:33:57

vivado2018.3电源规划与IO标准配置:全面讲解

Vivado 2018.3 中的电源规划与 IO 配置实战&#xff1a;从原理到避坑指南你有没有遇到过这样的情况&#xff1f;FPGA 逻辑功能明明写得没问题&#xff0c;仿真也通过了&#xff0c;可一上板子——HDMI 黑屏、DDR 接口时序紊乱、系统运行几分钟就发热重启。排查一圈下来&#xf…

作者头像 李华