如何使用Springfox?Swagger實(shí)現(xiàn)API自動生成單元測試
Springfox 簡介
Springfox 是一個使用Java語言開發(fā)開源的API Doc的框架, 它的前身是swagger-springmvc,可以將我們的Controller中的方法以文檔的形式展現(xiàn)。官方定義為: Automated JSON API documentation for API’s built with Spring。
Springfox 目前有1、2、3三種版本,從v3版本開始則有較大變化,據(jù)官方文檔介紹,變化如下:
刪除早期版本的一些依賴。特別是刪除springfox-swagger2和springfox-swagger-ui依賴。
- 刪除 @EnableSwagger2 注釋
- 添加 springfox-boot-starter 支持springboot使用的起步依賴
- Springfox 3.x 刪除了對 guava 和其他第三方庫的依賴(但仍然依賴于 spring 插件和開放 api 庫,用于注釋和模型)
Springfox 的作用
1)將前后端有效分離,并保證了API與文檔的實(shí)時同步
2)使用springfox生成的接口文檔直觀可視,支持查看各個接口需要的參數(shù)和返回結(jié)果
3)springfox支持在線測試,可實(shí)時檢查參數(shù)和返回值
接下來介紹如何使用Springfox Swagger實(shí)現(xiàn)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>
第二步:加入以下代碼,并作出適當(dāng)修改
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")) //根據(jù)自己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 不兼容的問題 * 復(fù)制即可 **/ @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: 作?在響應(yīng)的類上,對返回響應(yīng)數(shù)據(jù)的說明
- @ApiModelProerty:作?在類的屬性上,對屬性的說明
- @ApiOperation: 作?在具體?法上,對API接?的說明
- @ApiParam:作?在?法中的每?個參數(shù)上,對參數(shù)的屬性進(jìn)?說明
啟動程序,瀏覽器中輸?地址:http://127.0.0.1:端口號/swagger-ui/index.html ,可以正常并 顯?接?信息,說明配置成功,此時接?信息已經(jīng)顯?出來了,可以分別針對每個接?進(jìn)?測試,具 體操作按??指引即可。
另外:還可以導(dǎo)出到postman
1.復(fù)制
2.打開postman
3.粘貼
4.點(diǎn)擊import即可
到此這篇關(guān)于如何使用Springfox Swagger實(shí)現(xiàn)API自動生成單元測試的文章就介紹到這了,更多相關(guān)Springfox Swagger 單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java JDK動態(tài)代理(AOP)的實(shí)現(xiàn)原理與使用詳析
所謂代理,就是一個人或者一個機(jī)構(gòu)代表另一個人或者另一個機(jī)構(gòu)采取行動。下面這篇文章主要給大家介紹了關(guān)于Java JDK動態(tài)代理(AOP)實(shí)現(xiàn)原理與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07Springboot?中的?Filter?實(shí)現(xiàn)超大響應(yīng)?JSON?數(shù)據(jù)壓縮的方法
這篇文章主要介紹了Springboot?中的?Filter?實(shí)現(xiàn)超大響應(yīng)?JSON?數(shù)據(jù)壓縮,定義GzipFilter對輸出進(jìn)行攔截,定義 Controller該 Controller 非常簡單,主要讀取一個大文本文件,作為輸出的內(nèi)容,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10Java使用iTextPDF生成PDF文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Java使用iTextPDF生成PDF文件的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02應(yīng)用啟動數(shù)據(jù)初始化接口CommandLineRunner和Application詳解
這篇文章主要介紹了應(yīng)用啟動數(shù)據(jù)初始化接口CommandLineRunner和Application詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12mybatis的insert插入后獲取自增id的方法詳解(從controller到mapper)
這篇文章主要介紹了mybatis的insert插入后獲取自增id的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10Java如何使用Set接口存儲沒有重復(fù)元素的數(shù)組
Set是一個繼承于Collection的接口,即Set也是集合中的一種。Set是沒有重復(fù)元素的集合,本篇我們就用它存儲一個沒有重復(fù)元素的數(shù)組2022-04-04