在 Linux 环境下导出 Swagger 文档,常见做法取决于你使用的技术栈和Swagger 版本。下面按常见场景给你总结几种实用方式。
接口地址一般是:
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
接口地址:
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
如果你在浏览器访问:
http://ip:port/swagger-ui.html
可以:
api-docs 请求swagger.json导出一个 JSON 接口:
app.get('/api-docs.json', (req, res) => {
res.json(swaggerSpec);
});
Linux 导出:
curl http://localhost:3000/api-docs.json -o swagger.json
npm install -g swagger-cli
swagger-cli bundle swagger.yaml --outfile swagger.json
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 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 | 给非技术人员看 |
如果你能告诉我:
我可以给你更精确的命令。