当前位置: 首页 > 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>
     */
    @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

相关文章:

  • J6打卡——pytorch实现ResNeXt-50实现猴痘检测
  • 什么是 Perceptual Loss(感知损失)?
  • ForceMimic:以力为中心的模仿学习,采用力运动捕捉系统进行接触丰富的操作
  • webpack和vite的区别
  • pyspark 数据处理的三种方式RDD、DataFrame、Spark SQL案例
  • 大模型中的微调LoRA是什么
  • 多视图几何--对极几何--从0-1理解对极几何
  • 个人记录的一个插件,Unity-RuntimeMonitor
  • static 用法,函数递归与迭代详解
  • Spring Cloud之远程调用OpenFeign参数传递
  • Unity单例模式更新金币数据
  • CI/CD—Jenkins配置Poll SCM触发自动构建
  • DETR详解
  • 基于SpringBoot实现旅游酒店平台功能六
  • 【C#学习笔记02】基本元素与数据类型
  • mac本地部署Qwq-32b记录
  • 供应链工作效率如何提升
  • Java常见面试技术点整理讲解——后端框架(整理中,未完成)
  • 什么是一致性模型,在实践中如何选择?
  • 程序化广告行业(3/89):深度剖析行业知识与数据处理实践
  • 中疾控:适龄儿童要及时、全程接种百白破疫苗
  • 马上评|去年维修竣工的鼓楼,今年就“瀑布式落瓦”
  • MiniMax发布新一代语音大模型
  • 受贿2.61亿余元,陕西省政协原主席韩勇一审被判死缓
  • 印军称中国向巴基斯坦提供防空系统协助,外交部:中方十分重视与印、巴两国关系
  • 女巫的继承者们