springboot项目快速引入knife4j

  1. 引入依赖
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>
  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. 启动项目,访问http://localhost:8080/doc.html
    在这里插入图片描述

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

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

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

spring:
  # 支持 swagger3
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值