在 Linux 下测试 Swagger(OpenAPI)接口,常见做法有几种,取决于你是查看 Swagger 文档、调用接口,还是自动化测试。下面按常用场景给你一个完整说明。
如果你的后端已经集成 Swagger(如 Spring Boot / FastAPI / Gin + Swagger),通常可以直接访问:
http://服务器IP:端口/swagger-ui.html
# 或
http://服务器IP:端口/swagger-ui/
# 或
http://服务器IP:端口/docs
举例:
http://localhost:8080/swagger-ui.html
✅ 优点:
curl 测试接口(最常用)curl http://localhost:8080/v2/api-docs
# 或
curl http://localhost:8080/v3/api-docs
你会得到一个 OpenAPI JSON 文件。
例如接口是:
POST /api/user
测试:
curl -X POST http://localhost:8080/api/user \
-H "Content-Type: application/json" \
-d '{"name":"test","age":18}'
✅ 适合:
httpie(比 curl 更友好)安装:
sudo apt install httpie
# 或
sudo yum install httpie
测试:
http POST http://localhost:8080/api/user name=test age=18
curl http://localhost:8080/v3/api-docs > openapi.json
openapi-generator generate \
-i openapi.json \
-g python \
-o ./client
然后直接用生成的 SDK 测试。
✅ 适合:
docker run -d -p 8081:8080 swaggerapi/swagger-editor
访问:
http://localhost:8081
✅ 可:
wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gz
tar -xzf postman.tar.gz
./Postman/Postman
导入:
http://localhost:8080/v3/api-docs
| 类型 | 地址 |
|---|---|
| Swagger UI | /swagger-ui.html |
| API JSON | /v2/api-docs 或 /v3/api-docs |
测试:
curl http://localhost:8080/v3/api-docs
@EnableOpenApi
curl -H "Authorization: Bearer xxx" http://localhost:8080/api/test
| 场景 | 推荐方式 |
|---|---|
| 本机 / 有桌面 | 浏览器 Swagger UI |
| Linux 服务器 | curl / httpie |
| 自动化测试 | OpenAPI Generator |
| API 设计 | Swagger Editor |
| 复杂接口 | Postman |
如果你愿意,可以告诉我:
我可以直接给你针对你项目的测试命令。