如何从 Swagger UI 导出 Swagger JSON/YAML 文件? [英] How to export a Swagger JSON/YAML file from Swagger UI?

查看:311
本文介绍了如何从 Swagger UI 导出 Swagger JSON/YAML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何导出 Swagger 定义文件(应该是 JSON 或 YAML 文件)?

How can I export a Swagger definition file (it should be a JSON or YAML file)?

假设我有一个看起来像 http://example.com//swagger/ui/index#! 的端点:

Let's say I have an endpoint looking like http://example.com//swagger/ui/index#!:

版本为api version: v1.

没有导出"我可以看到的按钮.那么如何导出呢?

There is no "Export" button that I can see. So how do I export it?

推荐答案

API 定义的 URL 显示在 Swagger UI 的顶部栏中 - 在您的示例中是

The URL of the API definiton is displayed in the top bar of Swagger UI – in your example it's

/v2/api-docs?group=full-petstore-api

所以完整的 URL 看起来是

So the full URL appears to be

http://localhost:8080/v2/api-docs?group=full-petstore-api

在较新版本的 Swagger UI 中,API 定义的链接通常显示在 API 标题下方,因此您可以右键单击该链接并另存为.

In newer versions of Swagger UI, the link to the API definition is often displayed below the API title, so you can right-click the link and Save As.

如果您的 Swagger UI 没有指向 API 定义的可见链接,请查看页面源并查找 url 参数,例如:

If your Swagger UI does not have a visible link to the API definition, view the page source and look for the url parameter, such as:

const ui = SwaggerUIBundle({
  url: "https://petstore.swagger.io/v2/swagger.json",     // <-------
  dom_id: '#swagger-ui',

如果您没有看到 url 或者 url 是代码表达式,请打开浏览器开发工具,切换到 网络选项卡并禁用缓存.然后刷新页面,在HTTP请求中搜索API定义文件(swagger.jsonswagger.yamlapi-docs或类似文件).您可以按 XHR 过滤以缩小列表范围.

If you don't see the url or if url is a code expression, open the browser dev tools, switch to the Network tab and disable caching. Then refresh the page and search for the API definition file (swagger.json, swagger.yaml, api-docs or similar) among HTTP requests. You can filter by XHR to narrow down the list.

另一种查找实际 url 的方法是使用浏览器控制台并评估以下值之一,具体取决于您的 UI 版本:

Another way to find the actual url is to use the browser console and evaluate one of the following values, depending on your UI version:

  • Swagger UI 3.x:

  • Swagger UI 3.x:

ui.getConfigs().url

  • Swagger UI 2.x:

  • Swagger UI 2.x:

    swaggerUi.api.url
    

  • 有时 OpenAPI 定义可能嵌入在 .js 文件中——在这种情况下,使用该文件并去掉多余的部分.

    Sometimes the OpenAPI definition may be embedded within a .js file – in this case take this file and strip out the extra parts.

    这篇关于如何从 Swagger UI 导出 Swagger JSON/YAML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆