如何将Swagger API导入Postman?

mklgxw1f  于 2022-11-29  发布在  Postman
关注(0)|答案(7)|浏览(160)

最近我用 SpringMvc 和swagger-ui(v2)编写了休息API。

因此,我的问题是如何创建 Postman 需要的文件?
我不熟悉斯瓦格。

um6iljoc

um6iljoc1#

我在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左上角的“导入”按钮。
1.您将看到用于导入API文档的多个选项。单击“粘贴原始文本”。
1.将JSON格式粘贴到文本区域,然后单击导入。
1.您将看到所有的API都显示为“Postman Collection”,并且可以从Postman使用它。

您也可以使用“从链接导入”。在此处粘贴从Swagger或任何其他API文档工具生成JSON格式API的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;
fiei3ece

fiei3ece2#

有了.Net Core,现在一切变得非常简单:
1.您可以在自己的页面上找到JSON URL:

1.单击该链接并复制URL
1.现在转到Postman并点按“导入”:

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

ajsxfq5m

ajsxfq5m3#

接受的答案是正确的,但我将重写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:**安装完成,现在需要在controllers中记录API

@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:**您还可以通过http://localhost:8080/swagger-ui.html使用独立UI,而无需任何其他rest客户端,这非常好,您可以毫无麻烦地托管您的文档。

oipij1gg

oipij1gg4#

    • 建议您阅读此答案**

参考https://stackoverflow.com/posts/39072519答案,然后部分删除返回的内容,最后发现swagger缺少一些配置,postmat无法导入。
您需要在swagger中添加以下配置

@Bean
public Docket api(SwaggerProperties swaggerProperties) {
 swaggerProperties.setTitle("my-project");
 swaggerProperties.setDescription("my project");
 swaggerProperties.setVersion("v1");
 swaggerProperties.getContact().setUrl("http");
 //I overlooked other configurations. Note that my swagger configuration lacks these.
}

简而言之,为ApiInfoBuilder类中的属性分配尽可能多的值
Spring引导版本:2.3.10.发布Springfox-swagger版本:2.9.2

ctehm74n

ctehm74n5#

您可以这样做: Postman -〉导入-〉链接-〉{root_url}/v2/api-docs

avkwfej4

avkwfej46#

这就是我在Swagger编辑器界面中的工作方式:

方法1

YAML文件内容复制到Raw Text区域:

方法2(更多步骤)

步骤1:将文件导出为JSON

第2步:使用Postman "导入"导入JSON文件

bakd9h0s

bakd9h0s7#

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

您也可以在线获取一些示例swagger文件来验证这一点(如果您的swagger文档中有错误)。

相关问题