在 Debian 上“测 Swagger”通常有两种理解:
swagger-cli、swagger-codegen、Postman、curl 等)下面按最常见场景给你一个完整可操作的 Debian 示例。
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl wget openjdk-17-jre unzip nodejs npm
mkdir swagger-test
cd swagger-test
npm init -y
npm install express swagger-ui-express yamljs
swagger.yamlswagger: "2.0"
info:
title: Demo API
version: "1.0.0"
paths:
/hello:
get:
summary: Say hello
responses:
200:
description: OK
app.jsconst 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, () => {
console.log("Swagger UI: http://localhost:3000/api-docs");
});
node app.js
浏览器访问:
http://localhost:3000/api-docs
✅ 这就是在 Debian 上“测 Swagger UI”
如果 Swagger 已存在:
curl http://localhost:3000/hello
如果 Swagger 是 OpenAPI JSON:
curl http://localhost:8080/v3/api-docs
npm install -g swagger-cli
swagger-cli validate swagger.yaml
输出:
swagger.yaml is valid
docker run -p 8080:8080 \
-e SWAGGER_JSON=/swagger.yaml \
-v $(pwd):/usr/src/app \
swaggerapi/swagger-ui
访问:
http://localhost:8080
| 目的 | 工具 |
|---|---|
| 看接口文档 | Swagger UI |
| 校验 YAML | swagger-cli |
| 自动测试 | Postman / newman |
| 代码生成 | swagger-codegen |
| 压测 | locust / k6 |
如果你愿意,可以告诉我:
我可以直接给你对应 Debian 实操方案。