16

@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();
                                                
    }
}
Ana
  • 161
  • 1
  • 2
  • 9

6 Answers6

30
<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.

BsGhaz
  • 446
  • 4
  • 5
  • 3
    This is correct. For springfox-boot-starter 3.0.0 version, you should use swagger-ui 3.0.0 only and not 2.9.2 – Gagan May 06 '21 at 02:48
26

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>
Deb
  • 5,163
  • 7
  • 30
  • 45
  • That is not, what's mentioned in the answers to https://stackoverflow.com/q/62539860/7773582 and in the actual Github account https://github.com/springfox/springfox. But of course it is confusing; generally a more Spring integrated solution - when the Spring context is controlling what is possible - should do better. Therefore, ```springfox-boot-starter``` at least sounds more recommendable, doesn't it? – Jochen Haßfurter Oct 19 '20 at 12:14
5

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>
Sura
  • 51
  • 1
  • 3
2

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

Kejsi Struga
  • 550
  • 6
  • 21
0

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>
Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • Thank you, but still 'apis' method is giving the same error : The method apis(java.util.function.Predicate) in the type ApiSelectorBuilder is not applicable for the arguments (com.google.common.base.Predicate) – Ana Jul 31 '20 at 15:30
  • @Brinda you don't need to extends `WebMvcConfigurationSupport` – Eklavya Jul 31 '20 at 15:53
  • I have tried without "WebMvcConfigurationSupport" too. But still that error exists. I don't know why that error exists for me. – Ana Jul 31 '20 at 16:24
  • I can't reproduce your problem then after resolving those – Eklavya Jul 31 '20 at 16:28
  • @Eklavya: which Spring version and which dependencies (including swagger-ui) and also which Annotation for Configuration are you using? Can you paste your working example? Thank you! – Jochen Haßfurter Oct 19 '20 at 12:15
  • @JochenGebsattel Dependencies added – Eklavya Oct 19 '20 at 12:21
  • see my comment at https://stackoverflow.com/a/63650789/7773582 – Jochen Haßfurter Oct 20 '20 at 07:20
0

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>