Products
96SEO 2025-08-31 02:06 2
在软件开发过程中,API文档的生成和测试是至关重要的环节。Swagger作为一款强大的API文档生成和管理工具,可以帮助开发者轻松地生成API文档并进行自动化测试。本文将详细介绍如何在Linux系统中使用Swagger实现持续集成。
在开始之前, 请确保你的Linux系统已满足以下要求:
# 安装OpenJDK
sudo apt update
sudo apt install openjdk-11-jdk
# 安装Maven
sudo apt install maven
# 或者安装Gradle
sudo apt install gradle
在你的项目中添加Swagger依赖,
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
创建一个Swagger配置类来启用Swagger文档生成。
import io.swagger.v3.oas.models.OpenAPI;
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.oas.annotations.EnableOpenApi;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public OpenAPI api {
return new OpenAPI
.select
.apis)
.paths)
.build;
}
}
clean install
.gitlab-ci.yml
文件,并添加以下内容:.gitlab-ci.yml
文件到你的Git仓库。stages:
- build
- test
- document
build:
stage: build
script:
- mvn clean install
test:
stage: test
script:
- mvn test
document:
stage: document
script:
- mvn springdoc:generate
artifacts:
paths:
- target/generated-docs
http://your-application-url/swagger-ui
比方说如果你的应用程序运行在 localhost:8080
上,你可以访问:
http://localhost:8080/swagger-ui
API文档并进行测试。根据你的具体需求,可以选择使用Jenkins或GitLab CI/CD等工具来实现这一流程。
Demand feedback