当前位置: 首页 > news >正文

Spring Boot 3.x 整合 Swagger(springdoc-openapi)实现接口文档

本文介绍 Spring Boot 3.x 如何使用 springdoc-openapi 实现 Swagger 接口文档,包括版本兼容表、最简单的配置示例和常见错误解决方案。


1. Spring Boot 3.x 和 springdoc-openapi 版本对应表

Spring Boot 版本Spring Framework 版本推荐的 springdoc-openapi 版本
3.0.x / 3.1.x6.0.x / 6.1.x2.2.x ~ 2.5.x
3.2.x6.1.x2.5.x(最推荐)
3.3.x / 3.4.x6.2.x目前 2.5.x 不支持,等待 2.6.x

官方明确提示:springdoc 2.5.x 最高支持到 Spring 6.1.x。Spring Boot 3.3+ / 3.4+ 用 2.5.x 会出错。


2. Maven 依赖示例

(推荐)Spring Boot 3.2.x

确认你的父项目版本,例如:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.5</version>
</parent>

添加 springdoc 依赖:

<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.5.0</version>
</dependency>

(可选)Spring Boot 3.4.x(不稳定)

如果要强行在 3.4.x 用,可以试 SNAPSHOT:

<repositories><repository><id>sonatype-snapshots</id><url>https://oss.sonatype.org/content/repositories/snapshots/</url></repository>
</repositories><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.6.0-SNAPSHOT</version>
</dependency>

注意:不稳定,不建议生产环境使用。


3. 最简单的 Spring Boot + Swagger 配置

以下是一个最小可用的示例项目结构。

主类

@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

示例 Controller

@RestController
@RequestMapping("/api")
public class HelloController {@GetMapping("/hello")public String hello() {return "Hello, Springdoc OpenAPI!";}
}

可选的 Swagger 配置类

@Configuration
public class SwaggerConfig {@Beanpublic OpenAPI customOpenAPI() {return new OpenAPI().info(new Info().title("示例 API 文档").version("v1.0").description("Spring Boot 3 + Springdoc OpenAPI 实现的接口文档示例").contact(new Contact().name("开发者").email("example@example.com")).license(new License().name("Apache 2.0").url("http://springdoc.org")));}
}

4. 访问地址

启动后,默认访问:

  • Swagger UI 页面

    http://localhost:8080/swagger-ui/index.html
    
  • JSON 文档

    http://localhost:8080/v3/api-docs
    
  • YAML 文档

    http://localhost:8080/v3/api-docs.yaml
    

注意:路径是 /swagger-ui/index.html,不是老版本的 /swagger-ui.html


5. 常见错误和解决方案

错误:NoSuchMethodError: ControllerAdviceBean

原因:Spring Boot 3.3 / 3.4 用的是 Spring Framework 6.2,springdoc 2.5.x 不支持。

解决方法:

  • 降级到 Spring Boot 3.2.x
  • 或尝试使用 springdoc-openapi 2.6.0-SNAPSHOT

6. 总结

  • Spring Boot 3.0 ~ 3.2 推荐使用 springdoc-openapi 2.5.0,兼容稳定。
  • Spring Boot 3.3 / 3.4 官方2.5.x不支持,需等待2.6.x正式版。
  • 生产环境建议使用兼容的稳定版本组合。

如果需要更多内容(Gradle 版本、WebFlux 配置、多分组文档、认证方案等),欢迎留言讨论。

http://www.dtcms.com/a/268379.html

相关文章:

  • class类和style内联样式的绑定 + 事件处理 + uniapp创建自定义页面模板
  • React Ref 指南:原理、实现与实践
  • 深度学习篇---Yolov系列
  • 远程桌面启动工具
  • Flutter 每日翻译之 Widget
  • Day53GAN对抗生成网络思想
  • MySQL主从复制与读写分离概述
  • 一文了解PMI、CSPM、软考、、IPMA、PeopleCert和华为项目管理认证
  • Protein FID:AI蛋白质结构生成模型评估新指标
  • Redis-主从复制-分布式系统
  • 算法学习day15----蓝桥杯--进制转换
  • Web攻防-XMLXXE无回显带外SSRF元数据DTD实体OOB盲注文件拓展
  • 大数据Hadoop之——Flink1.17.0安装与使用(非常详细)
  • 桥梁桥拱巡检机器人cad+【4张】设计说明书+绛重+三维图
  • 了解微服务
  • JVM的内存区域划分,类加载器和GC
  • Modbus 与 BACnet 协议互操作:工业协议转换方案(一)
  • JavaSE -- 泛型详细介绍
  • 【机器学习笔记 Ⅱ】2 神经网络中的层
  • HCIA-生成数协议(STP)
  • Debezium日常分享系列之:Debezium管理平台
  • 【Elasticsearch入门到落地】15、DSL排序、分页及高亮
  • golang 协程 如何中断和恢复
  • WHAT - 依赖管理工具 CocoaPods
  • 从小白到进阶:解锁linux与c语言高级编程知识点嵌入式开发的任督二脉(1)
  • 如何确保Kafka集群的高可用?
  • 【MySQL】DTS机制对触发器时间的影响
  • Python-可视化学习笔记
  • 【机器学习笔记Ⅰ】3 代价函数
  • 空调和烘干机的使用