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设计与文档生成,常用工具及安装方式如下:
- Swagger Editor:在线编辑API规范的工具,支持实时验证。可通过npm全局安装或Docker运行:
# npm全局安装(需管理员权限) sudo npm install -g swagger-editor # 或使用Docker(更轻量) docker pull swaggerapi/swagger-editor docker run -d -p 8080:8080 swaggerapi/swagger-editor - Swagger UI:生成交互式API文档的工具,可将OpenAPI规范转换为可视化界面。安装方式类似:
# 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 - Swagger Codegen:根据OpenAPI规范生成客户端/服务器代码的工具(可选,用于快速生成项目骨架):
# 使用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文件,进行实时验证与编辑:
- 启动Swagger Editor后,在浏览器访问
http://localhost:8080(若使用Docker则为http://your_server_ip:8080); - 点击“File”→“Open File”,选择本地的
swagger.yaml文件; - 编辑过程中,Swagger Editor会实时检查语法错误(如缺少
responses字段、参数类型不合法),并提示修复建议。
5. 集成Swagger到项目中(以Spring Boot为例)
若项目基于Spring Boot框架,可通过以下步骤集成Swagger,实现文档自动生成:
- 添加依赖:在
pom.xml中添加Swagger2及Swagger UI依赖:<dependency> <groupId>io.springfoxgroupId> <artifactId>springfox-swagger2artifactId> <version>2.9.2version> dependency> <dependency> <groupId>io.springfoxgroupId> <artifactId>springfox-swagger-uiartifactId> <version>2.9.2version> dependency> - 配置Swagger:创建一个配置类(如
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(); } } - 启动项目并访问文档:启动Spring Boot应用后,在浏览器访问
http://localhost:8080/swagger-ui.html(若使用Docker部署项目,需将localhost替换为服务器IP),即可看到自动生成的交互式API文档。
6. 自动化文档维护
为保持API文档与代码同步,建议将Swagger集成到CI/CD流程中:
- 使用
swagger-codegen或openapi-generator在代码提交后自动生成文档(如HTML、Markdown格式); - 将生成的文档上传至项目的
docs目录或发布至内部Wiki平台; - 示例命令(生成HTML文档):
swagger-codegen generate -i swagger.yaml -l html -o ./docs/api
通过自动化流程,可避免手动更新文档的繁琐,确保文档始终与最新代码一致。
以上就是关于“如何在Linux上使用Swagger进行API设计规范遵循”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm