欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot與knife4j的整合使用過程

 更新時間:2024年08月12日 14:48:04   作者:小小李程序員  
Knife4j?是一個基于Swagger構建的開源?JavaAPI文檔工具,主要包括兩大核心功能:文檔說明和在線調試,這篇文章主要介紹了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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論