1. Overview
1.概述
In this article, we’ll see how to import Swagger APIs to Postman.
在这篇文章中,我们将看到如何将Swagger APIs导入Postman中。
2. Swagger and OpenAPI
2.Swagger和OpenAPI
Swagger is an open-source set of rules, specifications, and tools for developing and describing REST APIs. However, post-2021, OpenAPI refers to the industry-standard specifications, whereas Swagger refers to the tooling.
Swagger是一套开源的规则、规范和工具,用于开发和描述REST APIs。然而,在2021年之后,OpenAPI指的是行业标准规范,而Swagger指的是工具。
3. Postman
3.邮递员
Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration. We can use Postman to test our API without writing any code.
Postman是一个用于构建和使用API的API平台。Postman简化了API生命周期的每个步骤,并简化了协作。我们可以使用Postman来测试我们的API,而无需编写任何代码。
We can use either the standalone app or the browser extension.
我们可以使用独立的应用程序或浏览器扩展。
4. Application
4.应用
We can work with any existing application, or we can create a simple application from scratch that exposes REST APIs.
我们可以使用任何现有的应用程序,或者我们可以从头开始创建一个暴露REST API的简单应用程序。
4.1. Maven Dependencies
4.1.Maven的依赖性
We need to add a couple of dependencies for using Swagger with the Swagger-UI:
我们需要为使用Swagger和Swagger-UI添加几个依赖项。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
4.2. Java Configuration
4.2.Java配置
Swagger can be as easy to configure as:
Swagger的配置可以说是非常简单。
@Configuration
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
When we start the application, we can check the Swagger-UI and find the REST API description per controller:
当我们启动应用程序时,我们可以检查Swagger-UI,找到每个控制器的REST API描述。
We can also check the API docs that are generated for our REST APIs:
我们还可以检查为我们的REST APIs生成的API文档。
5. Importing into Postman
5.导入到Postman中
There are multiple ways to import the APIs into Postman, but in most cases, it requires that the Swagger or OpenAPI definition be available in some text format (for example, JSON).
有多种方法可以将API导入Postman,但在大多数情况下,它要求Swagger或OpenAPI定义以某种文本格式提供(例如,JSON)。
We can open Postman and navigate to the APIs option on the left, then click on Import to see the different options available:
我们可以打开Postman,导航到左边的APIs选项,然后点击Import,看看有哪些不同的选项。
5.1. Importing File
5.1.导入文件
If we have a Swagger JSON file available, we can import it via the file option in Postman:
如果我们有一个可用的Swagger JSON文件,我们可以通过Postman中的文件选项来导入它。
5.2. Importing Link
5.2.导入链接
If we have the Swagger-UI link, we can directly use the link to import the API into Postman.
如果我们有Swagger-UI的链接,我们可以直接使用该链接将API导入Postman中。
Copy the API link from Swagger-UI as below:
从Swagger-UI中复制API链接,如下所示。
And import it via the same link from Postman:
并通过同一链接从Postman导入。
5.3. Importing via Raw Text
5.3.通过原始文本导入
We can also just paste the JSON as raw text to import the APIs:
我们也可以直接将JSON作为原始文本粘贴来导入API。
5.4. Importing via Code Repository
5.4.通过代码库导入
To import APIs from repositories, we need to be logged in to Postman. To import from GitHub, as an example, let’s follow the below steps:
要从仓库导入API,我们需要登录到Postman。以从GitHub导入为例,让我们按照以下步骤进行。
- Navigate to the Code Repository tab.
- Click on GitHub.
- Confirm the GitHub account and authorize postmanlabs to access repositories. Once done, return to the Postman application for further steps.
- On Postman, select the organization, repository, and branch and click Continue.
- Confirm the APIs we need to import and click Import.
6. Conclusion
6.结语
In this article, we’ve looked into different ways to import our REST APIs into Postman.
在这篇文章中,我们研究了将我们的REST APIs导入Postman的不同方法。