如何将Swagger API导入Postman?


113

最近,我用SpringMvc和swagger-ui(v2)编写了宁静的API 。我注意到邮递员中的导入功能:

在此处输入图片说明

所以我的问题是如何创建Postman需要的文件?

我不熟悉Swagger。


6
伙计,这真是太棒了!
阿德林

Answers:


119

我使用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": {

    }
}

可以按以下方式将其导入Postman。

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

将JSON导入Postman

导入的API

您也可以使用“从链接导入”。在此处粘贴从Swagger或任何其他API文档工具生成API的JSON格式的URL。

这是我的文档(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;

1
谢谢,但是现在的问题是我怎么能从swagger-ui导出文件呢?该链接没有用。
Demon Coldmist

@DemonColdmist我添加了代码以生成API。基本上,它会扫描整个目录,检查批注并生成JSON / YAML输出。抱歉,但我尚未将Swagger与JAVA结合使用。
JDpawar

谢谢,如果可以用PHP导出,那么Java也可以。我将其翻译成Java。
Demon Coldmist

2
在使用springfox-swagger2依赖关系的Java应用中,您可以通过打开浏览器并转到localhost:8080 / v2 / api-docs
Nacho Mezzadra,

1
@JDpawar谢谢,导入成功,但是它没有在Postman中为任何POST API生成任何“ body”信息。有任何想法吗?
user1559625

32

有了.Net Core,现在非常容易:

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

在此处输入图片说明

  1. 单击该链接并复制URL
  2. 现在转到邮递员,然后单击导入:

在此处输入图片说明

  1. 选择所需的内容,最终得到一个不错的端点集合:

在此处输入图片说明


8

接受的答案是正确的,但我将重写完整的步骤 java

我目前使用Swagger V2Spring 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>

步骤2:添加配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {

     public static final Contact DEFAULT_CONTACT = new Contact("Usama Amjad", "https://stackoverflow.com/users/4704510/usamaamjad", "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);

    }
}

第3步:安装完成,现在您需要在其中记录APIcontrollers

    @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
    }

用法:

您可以通过http://localhost:8080/v2/api-docs复制文档并将其粘贴到Postman中来导入文档,从而访问文档。

在此处输入图片说明

可选的Swagger UI:您也可以通过任何其他Rest Client使用独立UI http://localhost:8080/swagger-ui.html,这非常好,您可以轻松地托管文档。

在此处输入图片说明


3
导入时出错:导入Swagger 2.0时出错:(非可移植的)参数必须具有(Patchable)parameter.type
Ramraj,

0

您可以这样做:邮递员->导入->链接-> {root_url}/v2/api-docs


-1
  • 单击橙色按钮(“选择文件”)
  • 浏览到Swagger文档(swagger.yaml)
  • 选择文件后,将在POSTMAN中创建一个新集合。它将包含基于您的端点的文件夹。

您还可以在线获取一些样本swagger文件进行验证(如果您的swagger文档中有错误)。


你能告诉我如何导出swagger.yaml吗?我在SpringMvc中使用swagger-ui。
Demon Coldmist

您想从哪里出口招摇?您是否已使用招摇来创作YAML?
Ashwini Kumar
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.