温馨提示×

如何导出linux swagger文档

小樊
37
2026-10-06 13:55:42
栏目: 智能运维

在 Linux 环境下导出 Swagger 文档,常见做法取决于你使用的技术栈和Swagger 版本。下面按常见场景给你总结几种实用方式。


一、Spring Boot(Java)项目导出 Swagger 文档

1️⃣ Swagger 2(springfox)

接口地址一般是:

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

导出 JSON:

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

导出为 HTML(可选):

npm install -g swagger-ui-export
swagger-ui-export swagger.json

2️⃣ Swagger 3(springdoc-openapi)

接口地址:

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

导出 JSON:

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

导出 YAML:

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

二、使用 swagger-ui 页面导出

如果你在浏览器访问:

http://ip:port/swagger-ui.html

可以:

  1. F12 → Network
  2. 找到 api-docs 请求
  3. 右键 → Save as → swagger.json

三、Node.js / Express 项目

使用 swagger-jsdoc + swagger-ui-express

导出一个 JSON 接口:

app.get('/api-docs.json', (req, res) => {
  res.json(swaggerSpec);
});

Linux 导出:

curl http://localhost:3000/api-docs.json -o swagger.json

四、使用命令行工具(推荐)

1️⃣ swagger-cli(通用)

npm install -g swagger-cli
swagger-cli bundle swagger.yaml --outfile swagger.json

2️⃣ openapi-generator(生成文档/客户端)

wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/7.0.0/openapi-generator-cli-7.0.0.jar

java -jar openapi-generator-cli-7.0.0.jar generate \
  -i openapi.json \
  -g html2 \
  -o swagger-html

五、Docker 环境导出

如果是容器内服务:

docker exec -it container_id curl http://localhost:8080/v3/api-docs -o /tmp/swagger.json
docker cp container_id:/tmp/swagger.json .

六、常见格式说明

格式 用途
json 程序解析、导入工具
yaml 易读、版本管理
html 给非技术人员看

如果你能告诉我:

  • 使用的语言(Java / Go / Node / Python)
  • Swagger 版本(2 / 3 / OpenAPI)
  • 是否 Docker

我可以给你更精确的命令。

0 踩