SpringBoot與knife4j的整合使用過程
在網上看了一堆 knife4j 的使用教程,很多都是報一堆錯誤,經過千方百次的嘗試,終于找到了合適的版本及其配置
版本
此處是 knife4j2.0.7 版本 SpringBoot2.3.5.RELEASE 版本
其他版本推薦
Spring Boot版本 | Knife4j Swagger2規(guī)范 |
1.5.x ~ 2.0.0 | <Knife4j 2.0.0 |
2.0 ~ 2.2 | Knife4j 2.0.0 ~ 2.0.6 |
2.2.x~2.4.0 | Knife4j 2.0.6 ~ 2.0.9 |
2.4.0~2.7.x | >=Knife4j 4.0.0 |
>= 3.0 | >=Knife4j 4.0.0 |
導入maven坐標
<!-- 導入knife4j2.0.7版本依賴 SpringBoot2.3.5.RELEASE 版本 --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.7</version> </dependency>
另外SpringBoot2.3.2.RELEASE ~ SpringBoot2.5.15版本與Knife4j2.0.7 ~ Knife4j3.0.3整合SpringBoot的起步依賴也是兼容的
編寫配置類
目錄結構如下:
配置類:
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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; @Configuration @EnableSwagger2WebMvc public class Knife4jConfiguration { // Student組的測試文檔 @Bean(value = "studentDocket") public Docket studentDocket() { Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title("測試學生端接口文檔") // 設置當前文檔的標題 .description("用于測試學生端的所有接口的文檔") //自定義文檔簡介 .termsOfServiceUrl("寫學生端人員的服務地址URL") //寫這個模塊功能的程序員相關的URL .contact("寫學生端人員的聯系方式(郵箱)") //寫這個模塊功能的程序員的email郵箱 .version("1.0") //指定當前文檔的版本 .build()) //分組名稱 .groupName("學生端") //設置當前組名稱 .select() //這里指定Controller掃描包路徑,"com.example.controller.student"是一個放Controller的包 .apis(RequestHandlerSelectors.basePackage("com.example.controller.student")) .paths(PathSelectors.any()) .build(); return docket; } // Teacher組的測試文檔 @Bean(value = "teacherDocket") public Docket teacherDocket() { Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title("測試教師端接口文檔") .description("用于測試教師端的所有接口的文檔") .termsOfServiceUrl("寫教師端人員的服務地址URL") .contact("寫教師端人員的聯系方式(郵箱)") .version("1.0") .build()) //分組名稱 .groupName("教師端") .select() //這里指定Controller掃描包路徑 .apis(RequestHandlerSelectors.basePackage("com.example.controller.teacher")) .paths(PathSelectors.any()) .build(); return docket; } }
StudentController學生控制層
TeacherController教師控制層
注解說明:
@Api :可以通過tags屬性描述當前控制層的相關信息
@ApiOperation:可以通過value屬性描述當前接口的功能
頁面效果
訪問地址:http://localhost:8080/doc.html (我的端口是8080,如果你修改了程序啟動端口,記得換成自己的端口)
學生端
教師端
到此這篇關于SpringBoot與knife4j的整合使用的文章就介紹到這了,更多相關SpringBoot 整合knife4j內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Springboot3集成Knife4j的步驟以及使用(最完整版)
- SpringBoot?Knife4j框架&Knife4j的顯示內容的配置方式
- springboot讀取bootstrap配置及knife4j版本兼容性問題及解決
- springboot3整合knife4j詳細圖文教程(swagger增強)
- springboot整合knife4j全過程
- knife4j?整合?springboot的過程詳解
- SpringBoot中使用Knife4J的解決方案
- springboot集成swagger3與knife4j的詳細代碼
- Springboot中整合knife4j接口文檔的過程詳解
- knife4j+springboot3.4異常無法正確展示文檔
相關文章
詳解Http請求中Content-Type講解以及在Spring MVC中的應用
這篇文章主要介紹了Http請求中Content-Type講解以及在Spring MVC中的應用的相關資料,需要的朋友可以參考下2017-02-02Spring Boot 集成Shiro的多realm實現以及shiro基本入門教程
這篇文章主要介紹了Spring Boot 集成Shiro的多realm實現以及shiro基本入門,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Java中報錯org.springframework.jdbc.UncategorizedSQLException的多種
本文主要介紹了Java中報錯org.springframework.jdbc.UncategorizedSQLException的多種解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06