在CentOS系統上進行Swagger測試前,需完成以下環境配置:
sudo yum update -y
確保系統軟件包為最新版本。sudo yum install gcc-c++ make -y
curl -sL https://rpm.nodesource.com/setup_14.x | bash - # 使用Node.js 14.x版本(兼容性更好)
sudo yum install nodejs -y
# 驗證安裝
node -v # 應輸出Node.js版本(如v14.xx.xx)
npm -v # 應輸出npm版本(如6.xx.xx)
sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker
Docker可簡化Swagger UI的部署流程,無需手動配置依賴:
docker pull swaggerapi/swagger-ui:v4.15.5 # 拉取最新穩定版鏡像
docker run -d -p 8080:8080 -e SWAGGER_FILE=/app/swagger.yaml -v /path/to/your/swagger.yaml:/app/swagger.yaml swaggerapi/swagger-ui:v4.15.5
其中:
-p 8080:8080
:將容器的8080端口映射到主機的8080端口;-e SWAGGER_FILE
:指定Swagger文檔路徑(需替換為你的swagger.yaml
/swagger.json
文件路徑);-v
:將主機上的Swagger文檔掛載到容器內。http://<CentOS服務器IP>:8080
,即可看到Swagger UI界面。若需更多定制化功能,可手動下載并配置Swagger Editor和Swagger UI:
mkdir -p /opt/swagger
cd /opt/swagger
wget https://github.com/swagger-api/swagger-editor/archive/v3.14.0.tar.gz
wget https://github.com/swagger-api/swagger-ui/archive/v3.34.0.tar.gz
tar -xzf v3.14.0.tar.gz
tar -xzf v3.34.0.tar.gz
cd /opt/swagger/swagger-editor-3.14.0
npm install -g http-server
http-server -p 8081 # 啟動Editor服務,訪問http://<IP>:8081編寫API文檔
cd /opt/swagger/swagger-ui-3.34.0
npm init -y
npm install express --save
mkdir public
cp -r dist/* public/ # 復制Swagger UI靜態文件到public目錄
# 編輯index.js(配置API文檔路徑)
vi index.js
在index.js
中添加以下內容:const express = require('express');
const app = express();
app.use('/static', express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.listen(3000, () => console.log('Swagger UI running on port 3000'));
啟動服務:node index.js
,訪問http://<IP>:3000
即可看到Swagger UI界面。http://<IP>:8081
)編寫API文檔,支持YAML或JSON格式。例如,一個簡單的swagger.yaml
內容如下:openapi: 3.0.0
info:
title: Test API
version: 1.0.0
paths:
/user/query:
get:
summary: Query user information
parameters:
- name: userId
in: query
required: true
schema:
type: integer
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
-e SWAGGER_FILE
指定文檔路徑;public
目錄(如/opt/swagger/public
),并修改public/index.html
中的SWAGGER_URL
變量,指向本地文檔(如http://<IP>:3000/static/your-swagger.yaml
)。http://<CentOS服務器IP>:8080
(Docker部署)或http://<IP>:3000
(手動部署),進入Swagger UI界面。/user/query
)。/user/query
),右側面板會顯示接口詳情(參數、請求方式、響應格式);userId: 1
);{"id":1,"name":"John"}
)。若需在終端中測試接口,可使用curl命令(無需依賴Swagger UI):
curl "http://<服務器IP>:<接口端口>/user/query?userId=1"
curl -X POST "http://<服務器IP>:<接口端口>/factory/insert" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "factoryName=TestFactory&no=123&remark=TestRemark"
curl -X POST "http://<服務器IP>:<接口端口>/material/selectAll" \
-H "Content-Type: application/json" \
-d '{"factory":"TestFactory","materialName":"TestMaterial","offset":0,"page":1}'
curl -X POST "http://<服務器IP>:<接口端口>/api/all/order/money" \
-F "file=@/path/to/local/file.xlsx" \
-F "startTime=2025-01-01" \
-F "endTime=2025-06-30"
firewall-cmd --add-port=8080/tcp --permanent
和firewall-cmd --reload
命令配置;