Swagger 接口文檔的整合:
- 引入依賴(Swagger 或 Knife4j)。
- 自定義 Swagger 配置類。
- 定義需要生成接口文檔的代碼位置(Controller)。
- 注意:線上環(huán)境不要把接口暴露出去?。。】梢酝ㄟ^在 SwaggerConfig 配置文件開頭加上 @Profile({“dev”, “test”}) 限定配置僅在部分環(huán)境開啟。
- 啟動接口文檔。
- 可以通過在 controller 方法上添加 @Api、@ApiImplicitParam(name = “name”,value = “姓名”,required = true) @ApiOperation(value = “向客人問好”) 等注解來自定義生成的接口描述信息
Swagger
Swagger 官網
- 依賴引入
<!-- swagger 接口文檔 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 創(chuàng)建 config 文件
package com.heo.matchmatebackend.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 自定義 Swagger 接口文檔的配置
*/
@Configuration // 配置類
@EnableSwagger2 // 開啟 swagger2 的自動配置
@Profile({"dev", "test"}) //版本控制訪問
public class SwaggerConfig {
@Bean(value = "defaultApi2")
public Docket docket() {
// 創(chuàng)建一個 swagger 的 bean 實例
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 配置接口信息
.select() // 設置掃描接口
// 配置如何掃描接口
.apis(RequestHandlerSelectors
//.any() // 掃描全部的接口,默認
//.none() // 全部不掃描
.basePackage("com.heo.matchmatebackend.controller") // 掃描指定包下的接口,最為常用
//.withClassAnnotation(RestController.class) // 掃描帶有指定注解的類下所有接口
//.withMethodAnnotation(PostMapping.class) // 掃描帶有只當注解的方法接口
)
.paths(PathSelectors
.any() // 滿足條件的路徑,該斷言總為true
//.none() // 不滿足條件的路徑,該斷言總為false(可用于生成環(huán)境屏蔽 swagger)
//.ant("/user/**") // 滿足字符串表達式路徑
//.regex("") // 符合正則的路徑
)
.build();
}
/**
* api 信息
* @return
*/
private ApiInfo apiInfo() {
Contact contact = new Contact(
"heo", // 作者姓名
"https://blog.csdn.net/XiugongHao", // 作者網址
"xxx@qq.com"); // 作者郵箱
return new ApiInfoBuilder()
.title("matchmate") // 標題
.description("matchmate 接口文檔") // 描述
.termsOfServiceUrl("https://blog.csdn.net/XiugongHao") // 跳轉連接
.version("1.0") // 版本
.contact(contact)
.build();
}
}
- yml 配置(如果 springboot version >= 2.6,需要添加如下配置 pathmatch)
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
profiles:
active: dev
- 最后運行啟動。
http://localhost:8080/api/swagger-ui.html
Knife4j
Knife4j 官網
- 依賴引入。
<!-- knife4j 接口文檔 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
- config 文件配置。
package com.heo.matchmatebackend.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 自定義 Swagger 接口文檔的配置
*/
@Configuration // 配置類
@EnableSwagger2 // 開啟 swagger2 的自動配置
@Profile({"dev", "test"}) //版本控制訪問
public class SwaggerConfig {
@Bean(value = "defaultApi2")
public Docket docket() {
// 創(chuàng)建一個 swagger 的 bean 實例
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 配置接口信息
.select() // 設置掃描接口
// 配置如何掃描接口
.apis(RequestHandlerSelectors
//.any() // 掃描全部的接口,默認
//.none() // 全部不掃描
.basePackage("com.heo.matchmatebackend.controller") // 掃描指定包下的接口,最為常用
//.withClassAnnotation(RestController.class) // 掃描帶有指定注解的類下所有接口
//.withMethodAnnotation(PostMapping.class) // 掃描帶有只當注解的方法接口
)
.paths(PathSelectors
.any() // 滿足條件的路徑,該斷言總為true
//.none() // 不滿足條件的路徑,該斷言總為false(可用于生成環(huán)境屏蔽 swagger)
//.ant("/user/**") // 滿足字符串表達式路徑
//.regex("") // 符合正則的路徑
)
.build();
}
/**
* api 信息
* @return
*/
private ApiInfo apiInfo() {
Contact contact = new Contact(
"heo", // 作者姓名
"https://blog.csdn.net/XiugongHao", // 作者網址
"xxx@qq.com"); // 作者郵箱
return new ApiInfoBuilder()
.title("matchmate") // 標題
.description("matchmate 接口文檔") // 描述
.termsOfServiceUrl("https://blog.csdn.net/XiugongHao") // 跳轉連接
.version("1.0") // 版本
.contact(contact)
.build();
}
}
- yml 配置。
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
profiles:
active: dev
- 啟動。
http://localhost:8080/api/doc.html#/home
文章來源:http://www.zghlxwxcb.cn/news/detail-808447.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-808447.html
到了這里,關于Swagger + Knife4j 接口文檔的整合的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!