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

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>*/@Overridepublic 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 和 Setterpublic 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 响应。

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

相关文章:

  • 《Vue3实战教程》2:Vue3快速上手
  • ubuntu 24.04.1安装FTP流程
  • 多功能护照阅读器港澳通行证阅读机RS232串口主动输出协议,支持和单片机/Linux对接使用
  • 5个用于构建Web应用程序的Go Web框架
  • Qt中的异步相关类
  • 浅谈仓颉语言的优劣
  • Linux 显示系统活动进程状态命令 ps 详细介绍
  • scala中正则表达式的使用
  • 数据分析和AI丨知识图谱,AI革命中数据集成和模型构建的关键推动者
  • cocos creator制作2dTop-down游戏(虚拟摇杆、地图加载)
  • SQL Server 批量插入数据的方式汇总及优缺点分析
  • linux上抓包RoCEv2
  • 【机器学习与数据挖掘实战】案例04:基于K-Means算法的信用卡高风险客户识别
  • UDP网络编程套接
  • 期权VIX指数构建与择时应用
  • QT笔记- QClipboard剪切板对QByteArray数据的复制与粘贴
  • Python使用PyMySQL操作MySQL完整指南
  • IAR中如何而将定义的数组放在指定的位置
  • 使用skywalking,grafana实现从请求跟踪、 指标收集和日志记录的完整信息记录
  • Ubuntu 20.04 24.04 双网卡 Bond 配置指南
  • 深度学习之目标检测篇——残差网络与FPN结合
  • 2024-2030全球及中国埋线针行业研究及十五五规划分析报告
  • 穷举vs暴搜vs深搜vs回溯vs剪枝专题一>子集
  • MES系统工作流的单元测试方案
  • 2.学习TypeScript 编译选项配置
  • 计算机网络之王道考研读书笔记-2
  • 【BUG】记一次context canceled的报错
  • [SWPUCTF 2022 新生赛]善哉善哉
  • 《PCI密码卡技术规范》题目
  • 城市大屏设计素材宝库:助力设计师高效创作