温馨提示×

debian swagger怎么导出

小樊
40
2026-09-25 23:07:06
栏目: 智能运维

在 Debian 上“导出 Swagger”通常指把 Swagger / OpenAPI 文档 导出成 JSON 或 YAML 文件,常见场景有几种,我按常见情况给你说明:


一、如果你用的是 Swagger UI(Web 页面)

1. 直接从浏览器导出

  1. 打开 Swagger UI 页面
    例如:http://localhost:8080/swagger-ui.html
  2. 页面顶部或底部通常有:
    • api-docs
    • 或 /v2/api-docs(Spring Boot)
  3. 直接访问:
    curl http://localhost:8080/v2/api-docs > swagger.json
    
    或:
    curl http://localhost:8080/v3/api-docs > openapi.json
    

✅ 得到 swagger.json 就是导出的 Swagger 文档


二、Spring Boot 项目(最常见)

1. 确认依赖

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
</dependency>

或(新版本):

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

2. 导出命令

# Swagger 2
curl http://localhost:8080/v2/api-docs -o swagger.json

# OpenAPI 3
curl http://localhost:8080/v3/api-docs -o openapi.json

三、使用 swagger-cli 导出(推荐)

1. 安装 Node(Debian)

sudo apt update
sudo apt install nodejs npm -y

2. 安装 swagger-cli

sudo npm install -g swagger-cli

3. 导出 / 校验

swagger-cli bundle swagger.json -o exported.yaml

四、导出为不同格式

格式 命令
JSON curl ... > swagger.json
YAML swagger-cli bundle swagger.json -o swagger.yaml
HTML 用 redoc 或 swagger-ui

五、如果是 Python(FastAPI / Flask)

FastAPI

curl http://localhost:8000/openapi.json > openapi.json

Flask + Flasgger

curl http://localhost:5000/apispec_1.json > swagger.json

六、常见坑

  • ❌ 404:路径不对(确认 /v2/api-docs 或 /v3/api-docs)
  • ❌ 空文档:服务没启动或 Swagger 没开启
  • ✅ Debian 只是系统,和导出方式无关

如果你能告诉我:

  • 用的是 Spring Boot / FastAPI / Node
  • Swagger 版本(2 还是 3)
  • 是否要 JSON / YAML / HTML

我可以给你一条精确命令。

0 踩