如何将Swagger API导入Postman?
最近我用SpringMvc和swagger ui(v2)编写了restful API。我注意到Postman中的导入功能: 所以我的问题是如何创建邮递员需要的文件如何将Swagger API导入Postman?,swagger,postman,Swagger,Postman,最近我用SpringMvc和swagger ui(v2)编写了restful API。我注意到Postman中的导入功能: 所以我的问题是如何创建邮递员需要的文件 我不熟悉Swagger。我从事PHP工作,并使用Swagger 2.0来记录API。 <?php require("vendor/autoload.php"); $swagger = \Swagger\scan('path_of_the_directory_to_scan'); header('Content-Type: ap
我不熟悉Swagger。我从事PHP工作,并使用Swagger 2.0来记录API。
<?php
require("vendor/autoload.php");
$swagger = \Swagger\scan('path_of_the_directory_to_scan');
header('Content-Type: application/json');
echo $swagger;
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": {
}
}
这可以导入到邮递员如下
<?php
require("vendor/autoload.php");
$swagger = \Swagger\scan('path_of_the_directory_to_scan');
header('Content-Type: application/json');
echo $swagger;
- 单击橙色按钮(“选择文件”)
- 浏览到Swagger文档(Swagger.yaml)
- 选择文件后,将在POSTMAN中创建一个新集合。它将包含基于端点的文件夹李>
您还可以在线获取一些样例swagger文件来验证这一点(如果您的swagger文档中有错误)。接受的答案是正确的,但我将重写java
的完整步骤
我目前正在使用Swagger V2
和Spring Boot 2
这是一个简单的三步过程
步骤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>
用法:
您可以从http://localhost:8080/v2/api-文档
只需复制并粘贴到Postman中即可导入收藏
可选的招摇过市用户界面:您也可以通过http://localhost:8080/swagger-html
而且非常好,您可以轻松地托管文档
使用.Net Core,现在非常简单:
您可以在您的招摇过市页面上找到JSON URL:
单击该链接并复制URL
现在转到“邮递员”并单击“导入”:
选择您需要的,您将得到一个很好的端点集合:
您可以这样做:邮递员->导入->链接->{root\u url}/v2/api文档
谢谢,但现在的问题是如何从swagger ui导出文件?链接没有用。@DemonColdmist我已经添加了生成api的代码。基本上,它扫描整个目录,检查注释并生成JSON/YAML输出。很抱歉,我没有在JAVA中使用Swagger。谢谢,如果它可以用PHP导出,JAVA也可以。我将把它翻译成Java。在使用springfox-Swaggger2依赖项的Java应用程序中,您可以通过打开浏览器并指向@JDpawar来获得要在Postman中导入的JSON。谢谢,导入成功,但它不会在Postman中为任何PostAPI生成任何“body”信息。有什么想法吗?你能告诉我如何导出swagger.yaml吗?我正在SpringMvc中使用swagger ui。你想从哪里导出swagger?你已经在用招摇过市来写你的YAML了吗?伙计,这真是太棒了!!!导入时出错:导入Swagger 2.0:(可修补)参数时出错。对于非正文参数,类型是必需的
@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
}