首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
SAAS
ToB门户
了解全球最新的ToB事件
论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
微博
Follow
记录
Doing
博客
Blog
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
排行榜
Ranklist
相册
Album
应用中心
qidao123.com ToB IT社区-企服评测·应用市场
»
论坛
›
数据库
›
SQL-Server
›
SpringBoot3整合FastJSON2怎样设置configureMessageConv ...
返回列表
发新帖
SpringBoot3整合FastJSON2怎样设置configureMessageConverters
[复制链接]
发表于 2025-10-27 09:22:03
|
显示全部楼层
|
阅读模式
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要
登录
才可以下载或查看,没有账号?
立即注册
×
在 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 zhengkai.blog.csdn.net
*/
@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/MySpringBoot
API
https://github.com/moshowgame/MySpringBoot
API
回复
使用道具
举报
返回列表
浏览过的版块
Oracle
分布式数据库
DevOps与敏捷开发
物联网
数据仓库与分析
区块链
云原生
主机安全
向量数据库
东湖之滨
+ 我要发帖
登录后关闭弹窗
登录参与点评抽奖 加入IT实名职场社区
去登录
微信订阅号
微信服务号
微信客服(加群)
H5
小程序
快速回复
返回顶部
返回列表