当前位置:   article > 正文

springboot项目快速引入knife4j_spring boot2.6 引入 knife4j

spring boot2.6 引入 knife4j
  1. 引入依赖
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  1. knife4j配置文件
    basePackage改为自己存放接口的包名
/**
 * Knife4j 接口文档配置
 * https://doc.xiaominfo.com/knife4j/documentation/get_start.html
 *
 */
@Configuration
@EnableSwagger2
@Profile({"dev", "test"})
public class Knife4jConfig {

    @Bean
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("接口文档")
                        .description("knife4j-demomo")
                        .version("1.0")
                        .build())
                .select()
                // 指定 Controller 扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.knife4jdemo.demos.web"))
                .paths(PathSelectors.any())
                .build();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  1. 启动项目,访问http://localhost:8080/doc.html
    在这里插入图片描述

如果启动项目的时候启动失败了,报的错可能是

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
  • 1

这个错误在springboot版本>=2.6.0的时候会引起,解决办法:
①、降低springboot版本
②、在application.yml中加入

spring:
  # 支持 swagger3
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  • 1
  • 2
  • 3
  • 4
  • 5

上述knife4j基于Swagger2.0,OpenAPI3.0是swagger是Swagger2.0的升级版,一般来说,建议使用OpenAPI3.0,下一篇文章介绍基于OpenAPI3.0的knife4j

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/352997
推荐阅读
相关标签
  

闽ICP备14008679号