温馨提示×

debian swagger怎样调试接口

小樊
67
2026-10-04 16:59:50
栏目: 智能运维

在 Debian 上“调试 Swagger 接口”通常分几种场景:看 Swagger UI、用命令行调接口、或本地起服务联调。下面按常见用法给你一份实用指南。


一、通过 Swagger UI 调试(最常用)

如果你已经有一个提供 Swagger 的服务(如 Spring Boot、FastAPI、Node 等):

  1. 启动服务(以 Spring Boot 为例)
java -jar app.jar
  1. 浏览器访问
http://localhost:8080/swagger-ui.html

或

http://localhost:8080/swagger-ui/index.html
  1. 在页面中:
  • 展开接口
  • 点 Try it out
  • 填参数
  • 点 Execute
  • 查看 Request / Response

✅ 适合快速验证接口是否正常


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

即使没有 Swagger UI,也可以直接调接口。

1️⃣ 用 curl 调试

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

POST + JSON:

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

查看 Swagger JSON:

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

2️⃣ 用 httpie(更友好)

安装:

sudo apt update
sudo apt install httpie

使用:

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

三、本地没有 Swagger?自己生成

Spring Boot

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency>

访问:

/v3/api-docs
/swagger-ui.html

FastAPI(Python)

pip install fastapi uvicorn

自动生成:

/docs
/openapi.json

四、调试技巧(很实用)

✅ 看实际请求

curl -v http://localhost:8080/api

✅ 抓包(高级)

sudo apt install tcpdump
sudo tcpdump -i lo port 8080

✅ 日志

  • Spring:application.yml 开 debug
  • Nginx:access.log

五、常见坑

❌ 跨域 → 后端加 CORS
❌ 404 → 路径或 context-path 错
❌ 401 → token / 权限问题
❌ Debian 防火墙

sudo ufw status

如果你能告诉我:

  • 用的是 什么后端(Java / Python / Node)
  • 是 本地调试还是服务器
  • 想用 UI 还是命令行

我可以直接给你“对应场景”的精确步骤。

0 踩