SpringBoot与knife4j的整合使用

        在网上看了一堆 knife4j 的使用教程,很多都是报一堆错误,经过千方百次的尝试,终于找到了合适的版本及其配置

版本

        此处是 knife4j2.0.7 版本 SpringBoot2.3.5.RELEASE 版本 

  其他版本推荐

        Spring Boot版本       Knife4j Swagger2规范
         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,如果你修改了程序启动端口,记得换成自己的端口)         

学生端

 教师端

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值