温馨提示×

debian swagger如何导出

小樊
42
2026-09-22 23:43:03
栏目: 智能运维

在 Debian 上“导出 Swagger”通常指把 Swagger/OpenAPI 文档(JSON 或 YAML)导出来,或者把接口定义生成代码/离线文档。下面按常见场景说明。


一、从运行的 Swagger 服务导出文档

1. 直接访问 API 文档地址

大多数 Swagger 项目会提供:

  • JSON: http://IP:端口/v2/api-docs
  • YAML: http://IP:端口/v2/api-docs.yaml

例如:

curl http://localhost:8080/v2/api-docs -o swagger.json
curl http://localhost:8080/v2/api-docs.yaml -o swagger.yaml

如果是 Swagger UI:

http://IP:端口/swagger-ui.html

二、Spring Boot 项目(最常见)

1. 使用 springfox / springdoc

  • springfox
    /v2/api-docs
    
  • springdoc-openapi
    /v3/api-docs
    /v3/api-docs.yaml
    

导出示例:

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

三、使用命令行工具导出(Debian)

1. 安装 wget / curl

sudo apt update
sudo apt install curl wget -y

2. 使用 Swagger CLI(可选)

npm install -g swagger-cli
swagger-cli validate swagger.yaml
swagger-cli bundle swagger.yaml -o swagger.json

四、将 Swagger 导出为离线 HTML / PDF

1. 使用 swagger-ui 静态导出

git clone https://github.com/swagger-api/swagger-ui.git
cp swagger.json swagger-ui/dist/
# 修改 index.html 指向 swagger.json

2. 使用 redoc

npm install -g redoc-cli
redoc-cli build swagger.yaml -o api.html

3. 转 PDF(可选)

apt install wkhtmltopdf
wkhtmltopdf api.html api.pdf

五、从代码生成 Swagger(反向导出)

如果是 Java 项目:

mvn springfox-swagger2:generate

或在代码中启用:

@EnableSwagger2

六、常见错误排查

  • 404:路径不对(v2/v3)
  • 无内容:服务未启动或权限限制
  • CORS 问题:用 curl 而不是浏览器

如果你能告诉我:

  • 是什么项目(Spring Boot / Node / Python)
  • Swagger 版本(1.x / 2 / 3)
  • 想导出成什么(JSON / YAML / HTML / 代码)

我可以给你精确到命令的示例。

0 踩