gpt4 book ai didi

swagger - 如何将 Swagger API 导入 Postman 中?

转载 作者:行者123 更新时间:2023-12-02 09:00:51 24 4
gpt4 key购买 nike

最近我使用 SpringMvc 和 swagger-ui(v2) 编写了 Restful API。我注意到 Postman 中的导入功能:

enter image description here

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

我对 Swagger 不熟悉。

最佳答案

我从事 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 作为“Postman Collection”,并且可以从 Postman 中使用它。

Importing the JSON into Postman

Imported APIs

您还可以使用“从链接导入”。此处粘贴从 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;

关于swagger - 如何将 Swagger API 导入 Postman 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39072216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com