最近我用SpringMvc和swagger-ui(v2)编写了restful api。我注意到Postman中的Import函数:

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

我不熟悉斯威格。


当前回答

推荐你看一下这个答案

https://stackoverflow.com/a/51072071/4712855

参考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-boot版本:2.3.10.RELEASE Springfox-swagger版本:2.9.2

其他回答

这是Swagger编辑器界面中对我有用的:

方法1

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

方法二(多步骤)

步骤1:将文件导出为JSON

步骤2:使用Postman“Import”导入JSON文件

我使用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 UI左上角的“Import”按钮。 您将看到用于导入API文档的多个选项。点击“粘贴原始文本”。 将JSON格式粘贴到文本区,然后单击导入。 你会看到你所有的api为“邮差收集”,并可以从邮差使用它。

你也可以使用“Import From Link”。在这里粘贴URL,从Swagger或任何其他API文档工具生成JSON格式的API。

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

现在使用。net Core非常简单:

你可以在swagger页面上找到JSON URL:

单击该链接并复制URL 现在转到邮差,点击导入:

选择你需要的,你最终会得到一个不错的端点集合:

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

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

你也可以在网上得到一些样本swagger文件来验证这一点(如果你的swagger文档中有错误)。