下面分**“用 Swagger 来写/展示 API 文档”和“在 Debian 上运行 Swagger 工具”**两部分说,都是 Debian(含 Ubuntu)环境下最常见的用法。
Swagger 现在通常指 OpenAPI + Swagger 工具链:
sudo apt update
sudo apt install docker.io -y
sudo systemctl enable --now docker
docker run -p 8080:8080 \
-e SWAGGER_JSON=/foo/openapi.yaml \
-v $PWD:/foo \
swaggerapi/swagger-ui
浏览器访问:
http://localhost:8080
sudo apt install nginx -y
cd /var/www/html
git clone https://github.com/swagger-api/swagger-ui.git
cp swagger-ui/dist/* .
# 修改 index.html 中的 url 指向你的 openapi.yaml
访问:
http://<debian-ip>/
docker run -p 8081:8080 swaggerapi/swagger-editor
访问:
http://localhost:8081
sudo apt install nodejs npm -y
npm init -y
npm install swagger-ui-express yamljs
const express = require("express");
const swaggerUi = require("swagger-ui-express");
const YAML = require("yamljs");
const swaggerDocument = YAML.load("./swagger.yaml");
const app = express();
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.listen(3000);
访问:
http://localhost:3000/api-docs
sudo apt install default-jre -y
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.41/swagger-codegen-cli-3.0.41.jar
java -jar swagger-codegen-cli-3.0.41.jar generate \
-i openapi.yaml \
-l python \
-o ./client
✅ Debian 无图形界面也能跑(Server 版)
✅ Swagger UI 只是静态页面
❌ Swagger ≠ 自动测试(那是 Postman / Dredd)
如果你告诉我:
我可以直接给你一份可复制粘贴的配置。