如何导入 Swagger API 到 Postman?

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

enter image description here

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

我不熟悉 Swagger。

222209 次浏览
我在 PHP上使用 Swagger 2.0来记录 API。 Swagger 文档是动态创建的(至少我在 PHP 中是这样使用的)。文档以 JSON 格式生成

. xml格式

示例文档

{
"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”左上角的“导入/Import”按钮。
  2. 您将看到用于导入 API 文档的多个选项。点击“粘贴原始文本(Paste Raw Text)”。
  3. 将 JSON 格式粘贴到文本区,然后单击导入。
  4. 你会看到你所有的api都是 'Postman 集合(Postman Collection)',并且可以在 Postman 中使用它。

 import JSON into Postman .

Imported APIs .

你也可以使用 “从链接导入(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;
  • 点击橙色按钮(“选择文件”)
  • 浏览到Swagger文档(Swagger .yaml)
  • 选择文件后,在POSTMAN中创建一个新的集合。它将包含基于端点的文件夹。

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

接受的答案是正确的,但我将重写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中导入集合。

enter image description here

你也可以通过http://localhost:8080/swagger-ui.html使用独立的UI,而不需要任何其他rest客户端,这非常好,你可以毫无麻烦地托管你的文档。

enter image description here

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

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

enter image description here

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

enter image description here

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

enter image description here

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

推荐你看一下这个答案

https://stackoverflow.com/a/51072071/4712855 < a href = " 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类中的属性被尽可能多地赋值

< p > spring-boot版本:2.3.10.RELEASE Springfox-swagger版本:2.9.2

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

方法1

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

enter image description here

方法二(多步骤)

步骤1:将文件导出为JSON

enter image description here

步骤2:用Postman导入JSON文件;

enter image description here