The method apis(java.util.function.Predicate<springfox.documentation.RequestHandler>) in the type ApiSelectorBuilder is not applicable

Viewed 17440

@Bean .api method is giving me below error. I have added Swagger config class with @Beans as usual.

The method apis(java.util.function.Predicate<springfox.documentation.RequestHandler>) in the type ApiSelectorBuilder is not applicable for the arguments (com.google.common.base.Predicate<springfox.documentation.RequestHandler>)

My Config class is below:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

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.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringFoxConfig extends WebMvcConfigurationSupport {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage(                                            
                    "package com.sample.controller;"))
                .paths(PathSelectors.any())
                .build();
                                                
    }
}
6 Answers
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

I tried these dependencies and the error did disappear.

Remove the following dependency:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

use the following:

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

This error happens if you add both "springfox-boot-starter" and "springfox-swagger-ui" dependency and if their versions are different.

Please note that if you are adding "springfox-boot-starter" then there is no need to add explicit "springfox-swagger-ui" dependency.

So just add the below and remove any "springfox-swagger-ui" dependency.

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
</dependency>

For those who already have the right dependencies:

 <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

You may need to add @EnabledWebMvc if you don't have it already.

Springfox documentation:

enter image description here

Remove package and ; semicolon to define basePackage for .apis()

RequestHandlerSelectors.basePackage("com.sample.controller")

And it seems you base package is com.sample then use that only

Full code:

@Configuration
@EnableSwagger2
public class SpringFoxConfig extends WebMvcConfigurationSupport {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sample.controller"))
                .paths(PathSelectors.any())
                .build();
                                                
    }
}

Using dependencies

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

Check the version of all your io.springfox:springfox-* jars in your external libraries. There must be multiple versions getting mixed up. If you are using springfox-boot-started:3.0.0, then exclude other versions by doing exclusion. You don't need any other springfox dependency. For me below fixed the issue -

      <groupId>com.abc.xyz</groupId>
      <artifactId>alpha-beta-api</artifactId>
      <exclusions>
        <exclusion>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger2</artifactId>
        </exclusion>
        <exclusion>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
        </exclusion>
        <exclusion>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-spring-web</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
Related