SpringBoot使用Swagger生成多模塊的API文檔
在大型項(xiàng)目中,通常會(huì)采用多模塊的架構(gòu)設(shè)計(jì),以提高代碼的可維護(hù)性和可擴(kuò)展性。使用 Swagger 為多模塊項(xiàng)目生成 API 文檔時(shí),需要進(jìn)行一些特殊的配置。以下以 Spring Boot 多模塊項(xiàng)目為例,詳細(xì)介紹如何使用 Swagger 生成多模塊的 API 文檔。
項(xiàng)目結(jié)構(gòu)
假設(shè)我們有一個(gè)包含多個(gè)模塊的 Spring Boot 項(xiàng)目,結(jié)構(gòu)如下:
multi-module-project
├── common-module
│ └── src
│ └── main
│ └── java
│ └── com
│ └── example
│ └── common
│ └── ...
├── module-a
│ └── src
│ └── main
│ └── java
│ └── com
│ └── example
│ └── modulea
│ └── ...
├── module-b
│ └── src
│ └── main
│ └── java
│ └── com
│ └── example
│ └── moduleb
│ └── ...
└── api-gateway
└── src
└── main
└── java
└── com
└── example
└── apigateway
└── ...
步驟 1:添加依賴
在每個(gè)需要生成 API 文檔的模塊的 pom.xml 中添加 Swagger 相關(guān)依賴:
<dependencies>
<!-- Swagger API 注解 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
步驟 2:配置 Swagger
在每個(gè)模塊中創(chuàng)建 Swagger 配置類,例如在 module-a 中:
package com.example.modulea.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.modulea.controller"))
.paths(PathSelectors.any())
.build();
}
}在 module-b 中也進(jìn)行類似的配置,只需將 basePackage 替換為 com.example.moduleb.controller。
步驟 3:統(tǒng)一 API 網(wǎng)關(guān)配置(可選)
如果項(xiàng)目中有 API 網(wǎng)關(guān)模塊(如 api-gateway),可以在該模塊中配置 Swagger,將各個(gè)模塊的 API 文檔聚合在一起。
package com.example.apigateway.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build();
}
}這里的 basePackage 設(shè)置為包含所有模塊控制器的根包,這樣 Swagger 會(huì)掃描該包下所有模塊的控制器,并生成統(tǒng)一的 API 文檔。
步驟 4:添加 API 注解
在每個(gè)模塊的控制器類和方法上添加 Swagger 注解,以描述 API 信息。例如在 module-a 的控制器中:
package com.example.modulea.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/module-a")
@Api(value = "Module A API", description = "這是 Module A 的 API 文檔")
public class ModuleAController {
@GetMapping("/hello")
@ApiOperation(value = "獲取 Module A 的問(wèn)候語(yǔ)", notes = "返回一個(gè)簡(jiǎn)單的問(wèn)候語(yǔ)")
public String hello() {
return "Hello from Module A!";
}
}在 module-b 的控制器中也進(jìn)行類似的操作。
步驟 5:查看 API 文檔
啟動(dòng)各個(gè)模塊的 Spring Boot 應(yīng)用程序,訪問(wèn)相應(yīng)模塊的 Swagger UI 地址(如 http://localhost:8080/module-a/swagger-ui.html 或 http://localhost:8080/api-gateway/swagger-ui.html,端口號(hào)和路徑根據(jù)實(shí)際情況修改),即可查看各個(gè)模塊或統(tǒng)一的 API 文檔。
注意事項(xiàng)
確保每個(gè)模塊的 Swagger 配置類中的 basePackage 配置正確,以確保能夠掃描到相應(yīng)模塊的控制器。
如果使用 API 網(wǎng)關(guān)進(jìn)行文檔聚合,要注意網(wǎng)關(guān)的路由配置,確保能夠正確訪問(wèn)各個(gè)模塊的 API。
可以根據(jù)需要對(duì) Swagger 的配置進(jìn)行定制,如設(shè)置文檔標(biāo)題、描述、版本等信息。
通過(guò)以上步驟,你可以為 Spring Boot 多模塊項(xiàng)目使用 Swagger 生成清晰、統(tǒng)一的 API 文檔,方便開(kāi)發(fā)和測(cè)試人員查看和使用。
到此這篇關(guān)于SpringBoot使用Swagger生成多模塊的API文檔的文章就介紹到這了,更多相關(guān)SpringBoot Swagger生成API文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+camunda實(shí)現(xiàn)工作流的流程分析
Camunda是基于Java語(yǔ)言,支持BPMN標(biāo)準(zhǔn)的工作流和流程自動(dòng)化框架,并且還支持CMMN規(guī)范,DMN規(guī)范,本文給大家介紹springboot+camunda實(shí)現(xiàn)工作流的流程分析,感興趣的朋友一起看看吧2021-12-12
Java Web開(kāi)發(fā)之基于Session的購(gòu)物商店實(shí)現(xiàn)方法
這篇文章主要介紹了Java Web開(kāi)發(fā)之基于Session的購(gòu)物商店實(shí)現(xiàn)方法,涉及Java針對(duì)session的操作及數(shù)據(jù)庫(kù)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
idea遠(yuǎn)程調(diào)試spark的步驟講解
今天小編就為大家分享一篇關(guān)于idea遠(yuǎn)程調(diào)試spark的步驟講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問(wèn)題
這篇文章主要介紹了Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java使用XML與注解方式實(shí)現(xiàn)CRUD操作代碼
MyBatis提供了靈活的配置和使用方式,使得數(shù)據(jù)庫(kù)操作更加簡(jiǎn)潔和高效,通過(guò)本文,我們介紹了如何使用MyBatis框架,通過(guò)XML映射文件和注解兩種方式來(lái)實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查操作,感興趣的朋友跟隨小編一起看看吧2024-02-02
Spring?Boot?3.x開(kāi)發(fā)中MySQL?8.x窗口函數(shù)在JPA中的使用限制問(wèn)題詳解
開(kāi)窗函數(shù)是在MySQL8.0以后才新加的功能,因此要想直接使用開(kāi)窗函數(shù),則mysql版本要8.0以上,這篇文章主要介紹了Spring Boot 3.x開(kāi)發(fā)中MySQL 8.x窗口函數(shù)在JPA中的使用限制問(wèn)題的相關(guān)資料,需要的朋友可以參考下2026-04-04

