在大型项目中,通常会采用多模块的架构设计,以提高代码的可维护性和可扩展性。使用 Swagger 为多模块项目生成 API 文档时,需要进行一些特殊的配置。以下以 Spring Boot 多模块项目为例,详细介绍如何使用 Swagger 生成多模块的 API 文档。
项目结构
假设我们有一个包含多个模块的 Spring Boot 项目,结构如下:
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:添加依赖
在每个需要生成 API 文档的模块的 pom.xml
中添加 Swagger 相关依赖:
<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
在每个模块中创建 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 {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.example.modulea.controller")).paths(PathSelectors.any()).build();}
}
在 module-b
中也进行类似的配置,只需将 basePackage
替换为 com.example.moduleb.controller
。
步骤 3:统一 API 网关配置(可选)
如果项目中有 API 网关模块(如 api-gateway
),可以在该模块中配置 Swagger,将各个模块的 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 {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.example")).paths(PathSelectors.any()).build();}
}
这里的 basePackage
设置为包含所有模块控制器的根包,这样 Swagger 会扫描该包下所有模块的控制器,并生成统一的 API 文档。
步骤 4:添加 API 注解
在每个模块的控制器类和方法上添加 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 的问候语", notes = "返回一个简单的问候语")public String hello() {return "Hello from Module A!";}
}
在 module-b
的控制器中也进行类似的操作。
步骤 5:查看 API 文档
启动各个模块的 Spring Boot 应用程序,访问相应模块的 Swagger UI 地址(如 http://localhost:8080/module-a/swagger-ui.html
或 http://localhost:8080/api-gateway/swagger-ui.html
,端口号和路径根据实际情况修改),即可查看各个模块或统一的 API 文档。
注意事项
-
确保每个模块的 Swagger 配置类中的
basePackage
配置正确,以确保能够扫描到相应模块的控制器。 -
如果使用 API 网关进行文档聚合,要注意网关的路由配置,确保能够正确访问各个模块的 API。
-
可以根据需要对 Swagger 的配置进行定制,如设置文档标题、描述、版本等信息。
通过以上步骤,你可以为 Spring Boot 多模块项目使用 Swagger 生成清晰、统一的 API 文档,方便开发和测试人员查看和使用。