1. 准备Linux环境基础依赖
在Linux系统上使用Swagger前,需安装Node.js(用于运行Swagger Editor/UI)、npm(Node.js包管理器)及Java(部分Swagger工具如swagger-codegen依赖Java)。以Ubuntu为例,可通过以下命令安装:
# 更新软件包索引
sudo apt update
# 安装Node.js和npm(推荐使用LTS版本)
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# 验证安装
node -v # 应输出Node.js版本(如v18.x.x)
npm -v # 应输出npm版本(如9.x.x)
# 安装Java(OpenJDK 11及以上,用于swagger-codegen)
sudo apt install -y openjdk-11-jdk
这些依赖是后续安装Swagger工具的前提。
2. 安装Swagger核心工具
Swagger提供多种工具用于API设计与文档生成,常用工具及安装方式如下:
# npm全局安装(需管理员权限)
sudo npm install -g swagger-editor
# 或使用Docker(更轻量)
docker pull swaggerapi/swagger-editor
docker run -d -p 8080:8080 swaggerapi/swagger-editor
# npm全局安装
sudo npm install -g swagger-ui
# 或从源码构建(需git)
git clone https://github.com/swagger-api/swagger-ui.git
cd swagger-ui
npm install
npm run build
# 使用npm安装
sudo npm install -g swagger-codegen
安装完成后,可通过swagger-editor --help、swagger-ui --help等命令验证工具是否可用。
3. 编写OpenAPI规范文件
OpenAPI规范(OAS)是Swagger的核心,采用YAML或JSON格式定义API的结构(路径、参数、响应等)。以下是一个符合规范的YAML示例(swagger.yaml):
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
description: A demo API for Linux + Swagger integration
servers:
- url: http://localhost:3000/api/v1
description: Local development server
paths:
/users:
get:
summary: Retrieve a list of users
description: Returns an array of user objects with basic info
responses:
'200':
description: Successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
id:
type: integer
format: int64
description: Unique identifier for the user
name:
type: string
example: John Doe
email:
type: string
format: email
example: john@example.com
规范文件需遵循OpenAPI 3.0+标准,明确描述API的路径(如/users)、请求方法(如GET)、参数(如路径参数{id})、响应(如200状态码对应的JSON结构)及数据模型(如User schema)。
4. 验证与编辑API规范
使用Swagger Editor打开swagger.yaml文件,进行实时验证与编辑:
http://localhost:8080(若使用Docker则为http://your_server_ip:8080);swagger.yaml文件;responses字段、参数类型不合法),并提示修复建议。5. 集成Swagger到项目中(以Spring Boot为例)
若项目基于Spring Boot框架,可通过以下步骤集成Swagger,实现文档自动生成:
pom.xml中添加Swagger2及Swagger UI依赖:<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
SwaggerConfig.java),启用Swagger并定义扫描范围: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.controller")) // 扫描controller包
.paths(PathSelectors.any())
.build();
}
}
http://localhost:8080/swagger-ui.html(若使用Docker部署项目,需将localhost替换为服务器IP),即可看到自动生成的交互式API文档。6. 自动化文档维护
为保持API文档与代码同步,建议将Swagger集成到CI/CD流程中:
swagger-codegen或openapi-generator在代码提交后自动生成文档(如HTML、Markdown格式);docs目录或发布至内部Wiki平台;swagger-codegen generate -i swagger.yaml -l html -o ./docs/api
通过自动化流程,可避免手动更新文档的繁琐,确保文档始终与最新代码一致。