1. 準備Linux環境基礎依賴
在Linux系統上,首先需要安裝Node.js和npm(Node.js包管理器),這是Swagger Editor和Swagger UI運行的基礎環境。以Ubuntu/Debian為例,可通過以下命令安裝:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - # 添加Node.js源
sudo apt-get install -y nodejs # 安裝Node.js及npm
安裝完成后,通過node -v
和npm -v
驗證安裝是否成功。
2. 安裝并啟動Swagger Editor(可選,用于編寫/編輯API定義)
Swagger Editor是在線編寫和驗證OpenAPI規范的工具,支持實時語法檢查和預覽。
sudo npm install -g swagger-editor # 全局安裝Swagger Editor
swagger-editor # 啟動本地服務(默認端口8080)
docker pull swaggerapi/swagger-editor:v4.6.0 # 拉取最新版鏡像
docker run -d -p 8080:8080 --name swagger-editor swaggerapi/swagger-editor:v4.6.0 # 運行容器
啟動后,通過瀏覽器訪問http://<服務器IP>:8080
即可使用Swagger Editor。3. 安裝并啟動Swagger UI(用于可視化展示API文檔)
Swagger UI是將OpenAPI規范(YAML/JSON)渲染為交互式文檔的工具,支持“Try it out”等功能。
sudo npm install -g swagger-ui-express express # 安裝依賴
mkdir swagger-ui-project && cd swagger-ui-project # 創建項目目錄
創建index.js
文件,配置靜態文件服務和Swagger UI:const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json'); // 引入OpenAPI定義文件
const app = express();
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); // 映射訪問路徑
app.listen(3000, () => console.log('Swagger UI運行在 http://localhost:3000/api-docs'));
啟動服務:node index.js
。docker pull swaggerapi/swagger-ui:v4.15.5 # 拉取鏡像
docker run -d -p 8081:8080 --name swagger-ui -e SWAGGER_FILE=/app/swagger.yaml -v /path/to/swagger.yaml:/app/swagger.yaml swaggerapi/swagger-ui:v4.15.5 # 掛載定義文件
訪問http://<服務器IP>:8081
即可查看文檔。4. 編寫OpenAPI規范文件(核心:定義API結構)
OpenAPI規范(YAML或JSON格式)是Swagger文檔的基礎,需描述API的路徑、操作、參數、響應、模型等信息。示例如下(swagger.yaml
):
swagger: '2.0'
info:
title: Linux API文檔示例
version: 1.0.0
description: 用于演示Linux環境下的API文檔共享
basePath: /api/v1
schemes:
- http
paths:
/users:
get:
summary: 獲取用戶列表
description: 返回所有用戶的ID和名稱
responses:
'200':
description: 成功獲取用戶列表
schema:
type: array
items:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: integer
example: 1
name:
type: string
example: 張三
將上述文件保存為swagger.yaml
(或swagger.json
),放置在項目根目錄或指定路徑。
5. 集成Swagger到應用(可選,自動生成文檔)
若使用Spring Boot框架,可通過springdoc-openapi
依賴自動生成OpenAPI規范,無需手動編寫:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
application.properties
):springdoc.api-docs.path=/api-docs # OpenAPI規范路徑
springdoc.swagger-ui.path=/swagger-ui # Swagger UI訪問路徑
http://<服務器IP>:8080/swagger-ui
即可查看自動生成的文檔。6. 共享API文檔的方法
http://<公網IP>:8080
(Swagger Editor)或http://<公網IP>:8081/api-docs
(Swagger UI)訪問。curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash # 安裝Cpolar
sudo systemctl start cpolar # 啟動服務
cpolar http 8080 # 映射Swagger Editor的8080端口
根據Cpolar返回的公網地址(如https://xxx.cpalr.com
)訪問文檔。server {
listen 80;
server_name your-domain.com;
location /api-docs {
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd; # 存放用戶名密碼的文件
proxy_pass http://localhost:3000; # 代理到Swagger UI服務
}
}
通過htpasswd
命令創建密碼文件:htpasswd -c /etc/nginx/.htpasswd admin
。通過以上步驟,即可在Linux環境下完成Swagger的部署與配置,實現API文檔的共享與協作。