SpringBoot3整合FastJSON2怎样设置configureMessageConverters

[复制链接]
发表于 2025-10-27 09:22:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在 Spring Boot 3 中整合 FastJSON 2 重要涉及到以下几个步调,包罗添加依靠、设置 FastJSON 作为 JSON 处理处罚器等。下面是具体的步调:
1. 添加依靠

起首,你须要在你的 pom.xml 文件中添加 FastJSON 2 的依靠。以下是 Maven 依靠的示例:
  1. <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2-extension-spring6 -->
  2. <dependency>
  3.     <groupId>com.alibaba.fastjson2</groupId>
  4.     <artifactId>fastjson2-extension-spring6</artifactId>
  5.     <version>2.0.53</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
  8. <dependency>
  9.     <groupId>com.alibaba.fastjson2</groupId>
  10.     <artifactId>fastjson2</artifactId>
  11.     <version>2.0.53</version>
  12. </dependency>
复制代码
2. 设置 FastJSON 作为 JSON 处理处罚器

在 Spring Boot 3 中,默认的 JSON 处理处罚器是 Jackson。假如你想使用 FastJSON 作为 JSON 处理处罚器,你须要创建一个设置类来注册 FastJSON 的 HttpMessageConverter。
  1. import com.alibaba.fastjson2.support.config.FastJsonConfig;
  2. import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.http.converter.HttpMessageConverter;
  7. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  8. import java.nio.charset.StandardCharsets;
  9. import java.util.Collections;
  10. import java.util.List;
  11. @Slf4j
  12. @Configuration
  13. public class Fastjson2Config implements WebMvcConfigurer {
  14.     /**
  15.      * Fastjson2Config
  16.      * @author zhengkai.blog.csdn.net
  17.      */
  18.     @Override
  19.     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  20.         FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  21.         FastJsonConfig config = new FastJsonConfig();
  22.         config.setDateFormat("yyyy-MM-dd HH:mm:ss");
  23.         // 其他配置...
  24.         converter.setFastJsonConfig(config);
  25.         converter.setDefaultCharset(StandardCharsets.UTF_8);
  26.         converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
  27.         converters.add(0, converter);
  28.         log.info("Fastjson2 Initial Done");
  29.     }
  30. }
复制代码
3. 使用 FastJSON2 举行 JSON 序列化和反序列化

在你的 Spring Boot 应用中,你可以直接使用 FastJSON 的 API 举行 JSON 的序列化和反序列化。比方:
  1. import com.alibaba.fastjson2.JSON;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class SampleController {
  6.     @GetMapping("/json")
  7.     public String getJson() {
  8.         MyObject obj = new MyObject();
  9.         obj.setName("Test");
  10.         obj.setValue(123);
  11.         // 使用 FastJSON 进行序列化
  12.         return JSON.toJSONString(obj);
  13.     }
  14.     public static class MyObject {
  15.         private String name;
  16.         private int value;
  17.         // Getter 和 Setter
  18.         public String getName() {
  19.             return name;
  20.         }
  21.         public void setName(String name) {
  22.             this.name = name;
  23.         }
  24.         public int getValue() {
  25.             return value;
  26.         }
  27.         public void setValue(int value) {
  28.             this.value = value;
  29.         }
  30.     }
  31. }
复制代码
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
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表