SpringBoot3整合FastJSON2怎样配置configureMessageConverters

打印 上一主题 下一主题

主题 993|帖子 993|积分 2979

在 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 <a href="https://zhengkai.blog.csdn.net/">zhengkai.blog.csdn.net</a>
  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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表