在 Linux 环境下测试 Swagger(OpenAPI)接口,通常有三种常见方式:
如果你的服务已经集成了 Swagger UI(如 Spring Boot、FastAPI、Node.js 等):
http://ip:端口/swagger-ui.html
或http://ip:端口/swagger-ui/index.html
如果是远程 Linux 服务器,可用:
ssh -L 8080:localhost:8080 user@server
然后本地浏览器访问 http://localhost:8080/swagger-ui.html
Swagger 本质就是 HTTP 接口。
curl http://localhost:8080/v3/api-docs
curl http://localhost:8080/api/user/1
curl -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"123456"}'
curl http://localhost:8080/api/user \
-H "Authorization: Bearer eyJhbGciOi..."
适合接口很多或写脚本测试。
pip install openapi-generator-cli
openapi-generator-cli generate \
-i http://localhost:8080/v3/api-docs \
-g python \
-o ./client
然后直接用生成的 SDK 写测试代码。
导入 Swagger JSON:
http://ip:port/v3/api-docs
✅ Linux 无界面
→ 用 curl 或 swagger JSON + Postman CLI
✅ 接口需要登录
→ 先调登录接口拿 token,再带 header
✅ 跨服务器访问不了
→ 检查防火墙 / 端口 / 反向代理
如果你愿意,可以告诉我:
我可以给你精确的测试命令。