以下是在Debian系統上安裝Swagger的步驟(以集成Swagger UI為例):
安裝基礎依賴
sudo apt update
sudo apt install -y nodejs npm
安裝Swagger工具
sudo npm install -g swagger-ui-express swagger-jsdoc
創建Swagger配置文件
swagger.json
或swagger.yaml
,定義API接口信息(示例內容可參考)。集成到應用
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const app = express();
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.listen(3000, () => console.log('Server running on port 3000'));
啟動服務并訪問
node your-app.js
http://localhost:3000/api-docs
查看Swagger UI界面。可選步驟:
sudo npm install -g swagger-editor
啟動后訪問http://localhost:8080
編輯API文檔。注:根據實際需求選擇工具(如需生成靜態文檔,可使用swagger-codegen
),部分步驟需根據項目類型(如Python/Java)調整。