swagger报错

解决此错误:无法将apis(com.google.common.base.Predicate)应用于(java.util.function.Predicate)

在本篇博客中,我将分享我在完成黑马项目苍穹外卖导入knife4j依赖过程中遇到的报错。

通过这篇博客,希望能帮助到其他遇到相似问题的,并加深自对问题的理解解决能力

报错描述

apis(com.google.common.base.Predicate<springfox.documentation.RequestHandler>)’ in ‘springfox.documentation.spring.web.plugins.ApiSelectorBuilder’ cannot be applied to '(java.util.function.Predicate<springfox.documentation.RequestHandler>)

苍穹外卖提供的knife4j依赖如下:

<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>knife4j-spring-boot-starter</artifactId>
   <version>3.0.2</version>
</dependency>

配置类如下:

  @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("苍穹外卖项目接口文档")
                .version("2.0")
                .description("苍穹外卖项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

在进行调试时发现这个依赖并未生效,访问doc.hmtl为404,想着可能是版本依赖问题,所以将版本改为了3.0.3,进行编译时

apis(RequestHandlerSelectors.basePackage(“com.sky.controller”))出现报错,apis(com.google.common.base.Predicate<springfox.documentation.RequestHandler>)’ in ‘springfox.documentation.spring.web.plugins.ApiSelectorBuilder’ cannot be applied to '(java.util.function.Predicate<springfox.documentation.RequestHandler>)传入参数与需要类型不一致导致,于是进行类型强转发现也没有用,于是查看pom.xml文件,发现导入了swagger2依赖,经过查询发现不能混合Springfox 2.9.2和knife4j依赖 ->删除springfox启动程序,reload后成功启动。

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

由于Knife4j 使用了 Java 8 函数库,而 springfox-swagger2 使用了 Guava 类库。在旧版本的 springfox-swagger2(例如2.9.2版本)中,RequestHandlerSelectors.basePackage方法期望传入com.google.common.base.Predicate<springfox.documentation.RequestHandler>而不是java.util.function.Predicate<springfox.documentation.RequestHandler>

通过以上方法,成功解决了这个报错,并顺进行了接口调试。希望这个经验可以对你有所帮助,如果有任何疑问或其他解决方法欢迎在评论中!

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值