温馨提示×

Linux Swagger怎么调试接口

小樊
44
2026-07-22 04:01:45
栏目: 智能运维

在 Linux 下调试 Swagger 接口,常见有几种场景,下面按最常用 → 进阶给你一套完整实用的方案。


一、通过 Swagger UI 直接调试(最简单 ✅)

1️⃣ 启动项目后访问 Swagger UI

通常地址是:

http://localhost:端口/swagger
http://localhost:端口/swagger-ui.html
http://localhost:端口/swagger/index.html

例如:

http://127.0.0.1:8080/swagger

2️⃣ 在 Swagger UI 页面调试

  • 找到接口
  • 点击 Try it out
  • 填写参数(query / path / body)
  • 点击 Execute

✅ 优点:不用额外工具,适合快速验证
❌ 缺点:不方便自动化


二、用 curl 调试 Swagger 接口(Linux 常用 ✅✅)

Swagger 本质上就是 HTTP 接口。

1️⃣ 查看 Swagger 文档

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

或:

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

2️⃣ 调用接口示例

GET 请求

curl "http://localhost:8080/api/user?id=1"

POST JSON

curl -X POST http://localhost:8080/api/user \
  -H "Content-Type: application/json" \
  -d '{"name":"tom","age":18}'

带 Token

curl http://localhost:8080/api/user \
  -H "Authorization: Bearer YOUR_TOKEN"

✅ 优点:Linux 服务器必会
✅ 适合 CI / 脚本 / 容器环境


三、用 Swagger Codegen 生成 SDK 调试(进阶)

1️⃣ 安装 swagger-codegen

sudo apt install swagger-codegen

2️⃣ 生成客户端

swagger-codegen generate \
  -i http://localhost:8080/v3/api-docs \
  -l java \
  -o ./client

支持语言:

  • java
  • python
  • go
  • js
  • csharp

✅ 适合:自动化测试 / 调用第三方接口


四、用 Postman / Insomnia(推荐)

虽然你是在 Linux,但完全可以:

# 下载 Postman
wget https://dl.pstmn.io/download/latest/linux64

或直接用 Insomnia(轻量):

snap install insomnia

✅ 优点:

  • 可视化
  • 可保存接口
  • 支持环境变量

五、查看 Swagger 接口不到?常见排查 ✅

1️⃣ 端口是否监听

netstat -tunlp | grep 8080
# 或
ss -tunlp | grep 8080

2️⃣ 防火墙

sudo ufw status
sudo ufw allow 8080

3️⃣ 接口路径不对

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

常见路径:

  • /v2/api-docs
  • /v3/api-docs
  • /swagger-resources

六、Spring Boot + Swagger 调试小技巧

1️⃣ 开启 debug 日志

logging:
  level:
    springfox: DEBUG

2️⃣ 接口返回 401 / 403

  • 检查是否加了:
@EnableSwagger2
@SecurityScheme
  • 或放行 swagger 路径

七、推荐调试路径(总结 ✅)

场景 推荐方式
快速验证 Swagger UI
Linux 服务器 curl
自动化 swagger-codegen
复杂接口 Postman / Insomnia
查问题 curl + 日志

如果你愿意,可以告诉我:

  • 后端语言(Java / Go / Python?)
  • Swagger 版本(2 / 3 / OpenAPI?)
  • 是否跑在 Docker 里

我可以直接给你针对你项目的调试命令

0