如何使用Springfox?Swagger實現(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與文檔的實時同步
2)使用springfox生成的接口文檔直觀可視,支持查看各個接口需要的參數(shù)和返回結(jié)果
3)springfox支持在線測試,可實時檢查參數(shù)和返回值
接下來介紹如何使用Springfox Swagger實現(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ù)的屬性進?說明
啟動程序,瀏覽器中輸?地址:http://127.0.0.1:端口號/swagger-ui/index.html ,可以正常并 顯?接?信息,說明配置成功,此時接?信息已經(jīng)顯?出來了,可以分別針對每個接?進?測試,具 體操作按??指引即可。

另外:還可以導(dǎo)出到postman
1.復(fù)制

2.打開postman

3.粘貼

4.點擊import即可

到此這篇關(guān)于如何使用Springfox Swagger實現(xiàn)API自動生成單元測試的文章就介紹到這了,更多相關(guān)Springfox Swagger 單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用MybatisPlus實現(xiàn)sql日志打印優(yōu)化
本文主要介紹了使用MybatisPlus實現(xiàn)sql日志打印優(yōu)化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08
二種jar包制作方法講解(dos打包jar eclipse打包jar文件)
這篇文章主要介紹了二種jar包制作方法講解:dos打包jar和eclipse打包jar文件,大家參考使用吧2013-11-11
SpringBoot實現(xiàn)點餐系統(tǒng)的登錄與退出功能流程詳解
結(jié)束了Springboot+MyBatisPlus也是開始了項目之旅,將從后端的角度出發(fā)來整理這個項目中重點業(yè)務(wù)功能的梳理與實現(xiàn)2022-10-10
學(xué)java得這樣學(xué),學(xué)習(xí)確實也得這樣
學(xué)java得這樣學(xué),學(xué)習(xí)東西確實也得這樣2008-02-02

