用户名
Email
论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
帖子
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
IT评测·应用市场-qidao123.com
»
论坛
›
软件与程序人生
›
DevOps与敏捷开发
›
SpringBoot3整合FastJSON2怎样配置configureMessageConv ...
SpringBoot3整合FastJSON2怎样配置configureMessageConverters
熊熊出没
金牌会员
|
2025-3-11 13:37:24
|
显示全部楼层
|
阅读模式
楼主
主题
993
|
帖子
993
|
积分
2979
在 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
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
回复
举报
0 个回复
倒序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
回帖后跳转到最后一页
发新帖
回复
熊熊出没
金牌会员
这个人很懒什么都没写!
楼主热帖
数据库的三大范式
创建SQL server服务器
在字节跳动干软件测试5年,4月无情被辞 ...
TortoiseGit间接处理linux目录下的仓库 ...
神经网络与深度学习
2年自动化测试经验,连基础的都不会, ...
django小项目 ----- 创建错题集 ...
Java 知识 - 接口代理
Java如何调用HttpURLConnection类模拟 ...
基于遗传算法的最优潮流问题的研究(Ma ...
标签云
运维
CIO
存储
服务器
快速回复
返回顶部
返回列表