目錄
1.?pom.xml中引用依賴
2. 引入相關的依賴
3. 編寫配置類
4.?application.yml 中添加配置
5. API 常用注解?
6. 訪問 API 列表
7.?API 導入 Postman
使用 Springfox Swagger生成 API,并導入 Postman,完成API單元測試。
Swagger 簡介:Swagger 是?套 API 定義的規范 ,按照這套規范的要求去定義接口及接口相關信息, 再通過可以解析這套規范工具,就可以生成各種格式的接口文檔,以及在線接口調試頁面,通過自動文檔的方式,解決了接口文檔更新不及時的問題。
Springfox 簡介:是對 Swagger 規范解析并生成文檔的?個實現。
1.?pom.xml中引用依賴
統?管理版本,在 properties 標簽中加入版本號:
<springfox-boot-starter.version>3.0.0</springfox-boot-starter.version>
3.0.0 版本對應 Spring Boot 2.6 之前的版本,但是隨著 Spring Boot 的更新?Springfox 并沒有進行同步的更新,所以存在一些兼容性問題,因此我們選擇使用 SpringBoot 2.7.6 版本。
2. 引入相關的依賴
<!-- API?檔?成,基于swagger2 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>${springfox-boot-starter.version}</version>
</dependency>
<!-- SpringBoot健康監控 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3. 編寫配置類
- 在com.bitejiuyeke.forum.config 包下新建SwaggerConfig.java
- 解決 SpringBoot 2.6.0 以上與 Springfox3.0.0 不兼容的問題,涉及 SpringBoot 版本升級過程中的一 些內部實現變化,具體說明在修改配置文件部分
?將以下代碼復制到?SwaggerConfig 類中:
package com.example.demo.controller;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*/@Beanpublic Docket createApi() {Docket docket = new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).paths(PathSelectors.any()).build();return docket;}// 配置API基本信息private ApiInfo apiInfo() {ApiInfo apiInfo = new ApiInfoBuilder().title("論壇系統API").description("論壇系統前后端分離API測試").contact(new Contact("Test","https://hello.fprum.com", "1111111111@qq.com")).version("1.0").build();return apiInfo;}/*** 解決SpringBoot 6.0以上與Swagger 3.0.0 不兼容的問題* 復制即可**/@Beanpublic WebMvcEndpointHandlerMappingwebEndpointServletHandlerMapping(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(), newEndpointLinksResolver(allEndpoints, basePath),shouldRegisterLinksMapping, null);}private boolean shouldRegisterLinksMapping(WebEndpointPropertieswebEndpointProperties, Environment environment,String basePath) {return webEndpointProperties.getDiscovery().isEnabled() &&(StringUtils.hasText(basePath)||ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}
}
4.?application.yml 中添加配置
- 在 spring 節點下添加 mvc 配置項
由于 SpringBoot 2.6 之后版本把 SpringMVC 路徑匹配策略修改為 MatchingStrategy. PATH_PATTERN_PARSER;而 Springfox-Swagger 還沒有更新版本,我們暫時先把 路徑匹配策略回退到之前的MatchingStrategy. ANT_PATH_MATCHER
mvc:path match:matching-strategy: ANT_PATH_MATCHER #Springfox-Swagger兼容性配置
5. API 常用注解?
- ?@Api: 作用在 Controller 上,對控制器類的說明
- tags="說明該類的作用,可以在前臺界面上看到的注解"
- tags="說明該類的作用,可以在前臺界面上看到的注解"
- ?@ApiModel: 作用在響應的類上,對返回響應數據的說明
- ?@ApiModelProerty:作用在類的屬性上,對屬性的說明
- ?@ApiOperation: 作用在具體方法上,對 API 接口的說明
- ?@ApiParam: 作用在方法中的每?個參數上,對參數的屬性進行說明
6. 訪問 API 列表
啟動程序,在瀏覽器中輸入網址:http://127.0.0.1:58080/swagger-ui/index.html
可以正常運行并顯示接口信息,說明配置成功,此時接口信息已經顯示出來了,可以分別針每個接口進測試,具操作按頁面指引即可。
點擊“測試接口”出現如下圖所示:?
選擇對應的 API 列表,點擊“Try it out”:?
點擊 Execute:?
圖中箭頭所指方向即為測試結果。
帶有參數的同樣可以進行測試:
7.?API 導入 Postman
獲取 API 地址,打開 Swagger 頁面的 API 資源地址并復制:
?在 Postman 中導入 url:
接下來輸入參數:
?