如何将swagger apis导出到邮递员

Dem*_*ist 60 swagger postman

最近我写了RESTful API中与用SpringMVC和招摇的UI(V2).今天,我只注意到按钮导入的postman.And我点击它上面,我刚才看到的评论" 导入邮差收集,环境,数据转储,卷曲命令,或RAML/WADL/Swagger(v1/v2)/ Runscope文件. " 在此输入图像描述

起初我已经搞砸了,但没有答案满足我的情况.

所以我的问题是如何创建邮递员需要的文件.顺便说一句,我不熟悉招摇.

JDp*_*war 67

我从事PHP工作,并使用Swagger 2.0来记录API.Swagger文档是动态创建的(至少这是我在PHP中使用的).该文档以JSON格式生成.

样本文件

{
    "swagger": "2.0",
    "info": {
    "title": "Company Admin Panel",
        "description": "Converting the Magento code into core PHP and RESTful APIs for increasing the performance of the website.",
        "contact": {
        "email": "jaydeep1012@gmail.com"
        },
        "version": "1.0.0"
    },
    "host": "localhost/cv_admin/api",
    "schemes": [
    "http"
],
    "paths": {
    "/getCustomerByEmail.php": {
        "post": {
            "summary": "List the details of customer by the email.",
                "consumes": [
                "string",
                "application/json",
                "application/x-www-form-urlencoded"
            ],
                "produces": [
                "application/json"
            ],
                "parameters": [
                    {
                        "name": "email",
                        "in": "body",
                        "description": "Customer email to ge the data",
                        "required": true,
                        "schema": {
                        "properties": {
                            "id": {
                                "properties": {
                                    "abc": {
                                        "properties": {
                                            "inner_abc": {
                                                "type": "number",
                                                    "default": 1,
                                                    "example": 123
                                                }
                                            },
                                            "type": "object"
                                        },
                                        "xyz": {
                                        "type": "string",
                                            "default": "xyz default value",
                                            "example": "xyz example value"
                                        }
                                    },
                                    "type": "object"
                                }
                            }
                        }
                    }
                ],
                "responses": {
                "200": {
                    "description": "Details of the customer"
                    },
                    "400": {
                    "description": "Email required"
                    },
                    "404": {
                    "description": "Customer does not exist"
                    },
                    "default": {
                    "description": "an \"unexpected\" error"
                    }
                }
            }
        },
        "/getCustomerById.php": {
        "get": {
            "summary": "List the details of customer by the ID",
                "parameters": [
                    {
                        "name": "id",
                        "in": "query",
                        "description": "Customer ID to get the data",
                        "required": true,
                        "type": "integer"
                    }
                ],
                "responses": {
                "200": {
                    "description": "Details of the customer"
                    },
                    "400": {
                    "description": "ID required"
                    },
                    "404": {
                    "description": "Customer does not exist"
                    },
                    "default": {
                    "description": "an \"unexpected\" error"
                    }
                }
            }
        },
        "/getShipmentById.php": {
        "get": {
            "summary": "List the details of shipment by the ID",
                "parameters": [
                    {
                        "name": "id",
                        "in": "query",
                        "description": "Shipment ID to get the data",
                        "required": true,
                        "type": "integer"
                    }
                ],
                "responses": {
                "200": {
                    "description": "Details of the shipment"
                    },
                    "404": {
                    "description": "Shipment does not exist"
                    },
                    "400": {
                    "description": "ID required"
                    },
                    "default": {
                    "description": "an \"unexpected\" error"
                    }
                }
            }
        }
    },
    "definitions": {

    }
}
Run Code Online (Sandbox Code Playgroud)

这可以导入Postman,如下所示.

  1. 单击Postman UI左上角的" 导入 "按钮.
  2. 您将看到导入API文档的多个选项.单击" 粘贴原始文本 ".
  3. 将JSON格式粘贴到文本区域中,然后单击"导入".
  4. 您将看到所有API为" 邮递员收藏 ",并可以使用邮递员.

将JSON导入Postman

导入的API

您也可以使用"从链接导入".这里粘贴从Swagger或任何其他API文档工具生成API的JSON格式的URL.

这是我的Document(JSON)生成文件.它是在PHP中.我不知道JAVA和Swagger.

<?php
require("vendor/autoload.php");
$swagger = \Swagger\scan('path_of_the_directory_to_scan');
header('Content-Type: application/json');
echo $swagger;
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但现在的问题是我如何从 swagger-ui 导出文件?并且该链接毫无用处。 (2认同)
  • 在使用springfox-swagger2依赖项的java应用程序中,您可以通过打开浏览器并转到http:// localhost:8080/v2/api-docs来获取Postman中导入的JSON,如本答案中所述 (2认同)

Mik*_*Mik 54

使用 .Net Core 现在很容易:

  1. 您可以在 swagger 页面上找到 JSON URL:

在此处输入图片说明

  1. 单击该链接并复制 URL
  2. 现在转到 Postman 并单击导入:

在此处输入图片说明

  1. 选择你需要的,你最终会得到一个很好的端点集合:

在此处输入图片说明


Usa*_*jad 10

接受的答案是正确的,但我将重写java.

我目前正在使用Swagger V2with Spring Boot 2,这是一个简单的 3 步过程。

步骤 1:pom.xml文件中添加所需的依赖项。第二个依赖项是可选的,只有在需要时才使用它Swagger UI

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

第二步:添加配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {

     public static final Contact DEFAULT_CONTACT = new Contact("Usama Amjad", "/sf/users/329315731/", "hello@email.com");
      public static final ApiInfo DEFAULT_API_INFO = new ApiInfo("Article API", "Article API documentation sample", "1.0", "urn:tos",
              DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());

    @Bean
    public Docket api() {
        Set<String> producesAndConsumes = new HashSet<>();
        producesAndConsumes.add("application/json");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(DEFAULT_API_INFO)
                .produces(producesAndConsumes)
                .consumes(producesAndConsumes);

    }
}
Run Code Online (Sandbox Code Playgroud)

第 3 步:设置完成,现在您需要将 API 记录在controllers

    @ApiOperation(value = "Returns a list Articles for a given Author", response = Article.class, responseContainer = "List")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
            @ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
    @GetMapping(path = "/articles/users/{userId}")
    public List<Article> getArticlesByUser() {
       // Do your code
    }
Run Code Online (Sandbox Code Playgroud)

用法:

您可以访问您的文档,http://localhost:8080/v2/api-docs只需将其复制并粘贴到 Postman 中即可导入收藏。

在此处输入图片说明

可选的 Swagger UI:您还可以使用独立的 UI,无需任何其他休息客户端http://localhost:8080/swagger-ui.html,这非常好,您可以轻松托管您的文档。

在此处输入图片说明

  • 导入时出错:导入 Swagger 2.0 时出错:(可修补)parameter.type 对于非主体参数是强制的 (3认同)