温馨提示×

debian怎样调试swagger接口

小樊
44
2026-09-04 19:48:45
栏目: 智能运维

在 Debian 上“调试 Swagger 接口”通常分几种场景,我按最常见用法给你说明,你可以对号入座。


一、你是“用 Swagger UI 调接口”(最常见)

1️⃣ 本地访问 Swagger UI

如果你的服务在 Debian 上运行,例如:

  • Spring Boot:http://ip:8080/swagger-ui.html
  • FastAPI:http://ip:8000/docs
  • Node/Express + swagger-ui-express:http://ip:3000/api-docs

在浏览器直接打开即可。

⚠️ 如果打不开:

curl http://localhost:8080/swagger-ui.html
  • 能通 → 防火墙 / 浏览器问题
  • 不通 → 服务没起来

2️⃣ 检查服务是否监听

ss -lntp | grep 8080

netstat -lntp | grep 8080

3️⃣ 防火墙(Debian)

sudo ufw status

开放端口:

sudo ufw allow 8080

二、用命令行调试 Swagger 接口(推荐)

✅ curl(最基础)

curl -X GET "http://localhost:8080/api/users" \
  -H "Authorization: Bearer token"

带 JSON:

curl -X POST "http://localhost:8080/api/login" \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"123456"}'

✅ httpie(更友好)

sudo apt install httpie

http GET http://localhost:8080/api/users
http POST http://localhost:8080/api/login username=admin password=123456

三、你是“写代码生成 Swagger,调试不通”

1️⃣ 查看 Swagger JSON

curl http://localhost:8080/v3/api-docs

curl http://localhost:8080/swagger/v1/swagger.json

确认:

  • paths 是否正确
  • 参数名是否一致
  • 是否缺少 token

四、你是“Swagger 页面打不开 / 报错”

常见原因

  1. 跨域 (CORS)
Access to fetch blocked by CORS
  1. 接口 401 / 403
  • token 没加
  • Swagger 被安全框架拦截
  1. Base URL 不对
  • nginx 反向代理路径问题

调试方法

curl -i http://localhost:8080/v3/api-docs

看 HTTP 状态码。


五、用代理调试(开发推荐)

mitmproxy(抓 Swagger 请求)

sudo apt install mitmproxy
mitmproxy

浏览器设代理,看请求/响应。


六、你是“Spring Boot + Swagger 调试”

常见命令:

journalctl -u your-service -f

或:

tail -f /var/log/your-app.log

七、推荐调试组合(Debian 下)

最实用

  • Swagger UI(看接口)
  • curl / httpie(调接口)
  • 日志(看后端)

你可以告诉我:

  1. 用的是什么框架(Spring / FastAPI / Node / Go)
  2. Swagger 打不开还是接口返回错误
  3. 报错信息是什么

我可以直接给你针对性命令

0