天天看點

将swagger-ui中的接口導入postman

1.簡介

swagger是一款深受廣大後端開發人員喜愛的工具,它能夠幫助使用者減少文檔的編寫,且能夠以可視化界面的形式呈現接口,友善測試。使用swagger唯一的缺陷在于需要用注解對每個接口進行解釋說明,無形中也增加一些代碼量。本文将詳細介紹将swagger聲明的接口快速導入postMan,快速進行測試。

2.swagger配置篇

(1)導入依賴jar包

首先在所要使用swagger的工程中導入相關依賴,主要的依賴有:

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
           

(2)編寫配置類

将swagger-ui中的接口導入postman

(3)啟動類上添加配置類掃描路徑和@EnableSwagger2注解,同時注意還需要添加controller類所在的掃描路徑,否則可能出現無法找到注解接口的情況。

3.controller類和其對應的方法進行注解

具體注解可檢視swagger的相關文檔。

将swagger-ui中的接口導入postman
将swagger-ui中的接口導入postman

4.通路swagger的可視化頁面

通路路徑在配置類中已經配置過:http://localhost:15031/swagger-ui.html#/

将swagger-ui中的接口導入postman

5.将接口導入到postMan

(1)找到上述文檔路徑,也就是上圖中的:http://localhost:15031/v2/api-docs,将其導入postMan。注意,導入時項目要處于運作狀态,否則導入不成功。

将swagger-ui中的接口導入postman

(2)導入成功後,根據需要配置具體測試環境和生産環境位址,友善測試不同環境的接口。

将swagger-ui中的接口導入postman
将swagger-ui中的接口導入postman
将swagger-ui中的接口導入postman

(3)測試具體接口

将swagger-ui中的接口導入postman