SpringBoot中整合knife4j接口文檔的實(shí)踐
在項(xiàng)目開發(fā)中,web項(xiàng)目的前后端分離開發(fā),APP開發(fā),需要由前后端工程師共同定義接口,編寫接口文檔,之后大家都根據(jù)這個(gè)接口文檔進(jìn)行開發(fā),到項(xiàng)目結(jié)束前都要一直維護(hù)
接口文檔使得項(xiàng)目開發(fā)過程中前后端工程師有一個(gè)統(tǒng)一的文件進(jìn)行溝通交流開發(fā),項(xiàng)目維護(hù)中或者項(xiàng)目人員更迭,方便后期人員查看、維護(hù)
一、界面先賞
1、首頁

2、接口文檔

3、調(diào)試

二、整合 knife4j
1、引入 maven 依賴
<!-- knife4j接口文檔 start --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> <!-- 避免版本沖突 --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>29.0-jre</version> </dependency>
一般情況我們只需要引入 knife4j 的依賴即可,但是有時(shí)會(huì)出現(xiàn) guava 的版本沖突,所以,我們把 guava 一起引入進(jìn)來
2、knife4j 配置文件
創(chuàng)建 Knife4jConfig 文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* knife4j 配置
*
* @Author Lizhou
*/
@Configuration
@EnableSwagger2
public class Knife4jConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zyxx"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot項(xiàng)目 后臺(tái)服務(wù)API接口文檔")
.description("使用 knife4j 搭建的后臺(tái)服務(wù)API接口文檔")
.termsOfServiceUrl("http://localhost:8080/")
.contact("lizhou")
.version("1.0.0")
.build();
}
}
整體配置與 Swagger2 幾乎一致,掃描 controller 所在的包
3、啟動(dòng)類
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* knife4j 配置
*
* @Author Lizhou
*/
@Configuration
@EnableSwagger2
public class Knife4jConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zyxx"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot項(xiàng)目 后臺(tái)服務(wù)API接口文檔")
.description("使用 knife4j 搭建的后臺(tái)服務(wù)API接口文檔")
.termsOfServiceUrl("http://localhost:8080/")
.contact("lizhou")
.version("1.0.0")
.build();
}
}
我們需要開放其靜態(tài)資源的訪問,啟動(dòng)類實(shí)現(xiàn) WebMvcConfigurer 接口,重寫 addResourceHandlers 方法
4、訪問文檔
啟動(dòng)項(xiàng)目,訪問路徑http://localhost:8080/doc.html
三、注意
訪問時(shí),如果提示 knife4j 文檔異常,檢查下自己的攔截器是否沒有放開 knife4j 所需要的請求

需要在攔截器,放開請求
package com.zyxx.common.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
/**
* 注冊攔截器
*
* @Author Lizhou
**/
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginHandlerInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration ir = registry.addInterceptor(loginHandlerInterceptor);
// 攔截路徑
ir.addPathPatterns("/*");
// 不攔截路徑
List<String> irs = new ArrayList<String>();
irs.add("/login");
irs.add("/api/*");
// 開放knife4j
irs.add("/doc.html");
irs.add("/service-worker.js");
irs.add("/swagger-resources");
ir.excludePathPatterns(irs);
}
}
四、使用
使用注解的方式與 swagger2 是一樣的
1、controller
import com.zyxx.common.utils.ResponseResult;
import com.zyxx.sbm.service.MgtUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* <p>
* 用戶信息表 前端控制器
* </p>
*
* @author lizhou
* @since 2020-06-30
*/
@Api(tags = "后臺(tái)管理端--用戶模塊")
@Controller
@RequestMapping("/mgt-user")
public class MgtUserController {
@Autowired
private MgtUserService mgtUserService;
@ApiOperation(value = "分頁查詢用戶數(shù)據(jù)", notes = "分頁查詢用戶數(shù)據(jù)")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "頁碼數(shù)", required = true),
@ApiImplicitParam(name = "limit", value = "每頁條數(shù)", required = true)
})
@GetMapping("list")
@ResponseBody
public ResponseResult listUser(int page, int limit) {
return mgtUserService.listUser(page, limit);
}
}
@Api,整個(gè)類的注釋
@ApiOperation,方法上的注釋
@ApiImplicitParams,參數(shù)列表的注釋
@ApiImplicitParam,每一個(gè)參數(shù)的注釋
2、實(shí)體類
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
* 用戶信息表
* </p>
*
* @author lizhou
* @since 2020-06-30
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="mgt_user對象", description="用戶信息對象")
public class MgtUser extends Model<MgtUser> {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty(value = "主鍵ID")
private Long id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "年齡")
private Integer age;
@ApiModelProperty(value = "技能")
private String skill;
@ApiModelProperty(value = "評價(jià)")
private String evaluate;
@ApiModelProperty(value = "分?jǐn)?shù)")
private Long fraction;
@Override
protected Serializable pkVal() {
return this.id;
}
}
@ApiModel,實(shí)體類的注解
@ApiModelProperty,字段的注解
到此這篇關(guān)于SpringBoot中整合knife4j接口文檔的實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot整合knife4j內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java input 調(diào)用手機(jī)相機(jī)和本地照片上傳圖片到服務(wù)器然后壓縮的方法
今天小編就為大家分享一篇java input 實(shí)現(xiàn)調(diào)用手機(jī)相機(jī)和本地照片上傳圖片到服務(wù)器然后壓縮的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
idea中Java實(shí)體類怎樣生成序列化的版本號(hào)的方法
這篇文章主要介紹了idea中Java實(shí)體類怎樣生成序列化的版本號(hào)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot集成WebSocket的兩種方式(JDK內(nèi)置版和Spring封裝版)
這篇文章主要介紹了SpringBoot集成WebSocket的兩種方式,這兩種方式為JDK內(nèi)置版和Spring封裝版,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例
這篇文章主要介紹了Spring Boot項(xiàng)目維護(hù)全局json數(shù)據(jù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
WebUploader實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了WebUploader實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
java基本教程之Thread中start()和run()的區(qū)別 java多線程教程
這篇文章主要介紹了Thread中start()和run()的區(qū)別,Thread類包含start()和run()方法,它們的區(qū)別是什么?下面將對此作出解答2014-01-01
Java使用WebView實(shí)現(xiàn)桌面程序的技術(shù)指南
在現(xiàn)代軟件開發(fā)中,許多應(yīng)用需要在桌面程序中嵌入 Web 頁面,例如,你可能需要在 Java 桌面應(yīng)用中嵌入一部分 Web 前端,或者加載一個(gè) HTML5 界面以增強(qiáng)用戶體驗(yàn),所以本文給大家介紹了Java使用WebView實(shí)現(xiàn)桌面程序的技術(shù)指南,需要的朋友可以參考下2025-05-05

