如何使用Springfox?Swagger實現API自動生成單元測試
Springfox 簡介
Springfox 是一個使用Java語言開發(fā)開源的API Doc的框架, 它的前身是swagger-springmvc,可以將我們的Controller中的方法以文檔的形式展現。官方定義為: Automated JSON API documentation for API’s built with Spring。
Springfox 目前有1、2、3三種版本,從v3版本開始則有較大變化,據官方文檔介紹,變化如下:
刪除早期版本的一些依賴。特別是刪除springfox-swagger2和springfox-swagger-ui依賴。
- 刪除 @EnableSwagger2 注釋
- 添加 springfox-boot-starter 支持springboot使用的起步依賴
- Springfox 3.x 刪除了對 guava 和其他第三方庫的依賴(但仍然依賴于 spring 插件和開放 api 庫,用于注釋和模型)
Springfox 的作用
1)將前后端有效分離,并保證了API與文檔的實時同步
2)使用springfox生成的接口文檔直觀可視,支持查看各個接口需要的參數和返回結果
3)springfox支持在線測試,可實時檢查參數和返回值
接下來介紹如何使用Springfox Swagger實現API自動生成單元測試。
第一步:在pom.xml中添加依賴
<!-- API?檔?成,基于swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- SpringBoot健康監(jiān)控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>第二步:加入以下代碼,并作出適當修改
package com.bitejiuyeke.forum.config;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Swagger配置類
*/
// 配置類
@Configuration
// 開啟Springfox-Swagger
@EnableOpenApi
public class SwaggerConfig {
/**
* Springfox-Swagger基本配置
* @return
*/
@Bean
public Docket createApi() {
Docket docket = new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.btjyk.forum.controller")) //根據自己controller包的 路徑自行修改
.paths(PathSelectors.any())
.build();
return docket;
}
// 配置API基本信息
private ApiInfo apiInfo() {//以下 基本信息均可修改
ApiInfo apiInfo = new ApiInfoBuilder()
.title("線上論壇系統(tǒng)API")
.description("線上論壇系統(tǒng)前后端分離API測試")
.contact(new Contact("Bit Tech",
"https://edu.btjyk.com", "1598374550@qq.com"))
.version("1.0")
.build();
return apiInfo;
}
/**
* 解決SpringBoot 6.0以上與Swagger 3.0.0 不兼容的問題
* 復制即可
**/
@Bean
public WebMvcEndpointHandlerMapping
webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties, Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
Collection<ExposableWebEndpoint> webEndpoints =
webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping =
this.shouldRegisterLinksMapping(webEndpointProperties, environment,
basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints,
endpointMediaTypes,
corsProperties.toCorsConfiguration(), new
EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled()
&& (StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
}第三步:在application.yaml中添加
spring
mvc:
pathmatch:
matching-strategy: ant_path_matcher #Springfox-Swagger兼容性配置第四步:添加注解
- @Api: 作?在Controller上,對控制器類的說明 。tags="說明該類的作?,可以在前臺界?上看到的注解"
- @ApiModel: 作?在響應的類上,對返回響應數據的說明
- @ApiModelProerty:作?在類的屬性上,對屬性的說明
- @ApiOperation: 作?在具體?法上,對API接?的說明
- @ApiParam:作?在?法中的每?個參數上,對參數的屬性進?說明
啟動程序,瀏覽器中輸?地址:http://127.0.0.1:端口號/swagger-ui/index.html ,可以正常并 顯?接?信息,說明配置成功,此時接?信息已經顯?出來了,可以分別針對每個接?進?測試,具 體操作按??指引即可。

另外:還可以導出到postman
1.復制

2.打開postman

3.粘貼

4.點擊import即可

到此這篇關于如何使用Springfox Swagger實現API自動生成單元測試的文章就介紹到這了,更多相關Springfox Swagger 單元測試內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java JDK動態(tài)代理(AOP)的實現原理與使用詳析
所謂代理,就是一個人或者一個機構代表另一個人或者另一個機構采取行動。下面這篇文章主要給大家介紹了關于Java JDK動態(tài)代理(AOP)實現原理與使用的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07
Springboot?中的?Filter?實現超大響應?JSON?數據壓縮的方法
這篇文章主要介紹了Springboot?中的?Filter?實現超大響應?JSON?數據壓縮,定義GzipFilter對輸出進行攔截,定義 Controller該 Controller 非常簡單,主要讀取一個大文本文件,作為輸出的內容,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10
應用啟動數據初始化接口CommandLineRunner和Application詳解
這篇文章主要介紹了應用啟動數據初始化接口CommandLineRunner和Application詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
mybatis的insert插入后獲取自增id的方法詳解(從controller到mapper)
這篇文章主要介紹了mybatis的insert插入后獲取自增id的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10

