65

Is there a way i can remove the "basic-error-controller" from springfox swagger-ui?

Picture:

enter image description here

Rajkishan Swami
  • 3,569
  • 10
  • 48
  • 68

9 Answers9

106

You can restrict the request handler selector to scan only the package of your project:

    return new Docket( DocumentationType.SWAGGER_2)
        .select()
        .apis( RequestHandlerSelectors.basePackage( "your package" ) )
        ...
Carsten Zeitz
  • 1,076
  • 1
  • 8
  • 5
  • 2
    for anyone who isn't familiar with what to fill in for "your package", it's the folder name right underneath `java` – notacorn Jun 17 '20 at 17:34
  • 2
    That wasn't needed. Sometimes it's not clear what someone means when there's just text that doesn't look like the real value. Also, even though, they probably could have figured it out from reading the docs or the code, not everyone is as amazing as you. – Jeff Walker Sep 08 '20 at 15:31
  • Why was even the basic error controller added? Is it because when there is no controller to show something in swagger? – Satish Patro Nov 15 '21 at 14:02
45

I think, the most elegant solution is to include only @RestController controllers into swagger, only thing to bear in mind, is to annotate all the REST controllers with that annotation:

new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .paths(PathSelectors.any())
            .build();

As BasicErrorController is annotated with @Controller only, swagger would avoid BasicErrorController in definition file. Of course you can use your custom annotation instead of @RestController to mark your REST controllers as controllers eligible by swagger.

bladekp
  • 1,529
  • 22
  • 29
27
  • It can be done using Predicate.not() .

     @Bean
     public Docket api() {
         return new Docket(DocumentationType.SWAGGER_2)
             .select()
             .apis(RequestHandlerSelectors.any())
             .paths(PathSelectors.any())
             .paths(Predicate.not(PathSelectors.regex("/error.*")))
             .build();
     }
    
Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197
pvrforpranavvr
  • 2,708
  • 2
  • 24
  • 34
9

For example if your parent Package is com.app.microservice

package com.app.microservice;

Then use the following code it will only display the Controllers within the Package and disable/exclude others

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.app.microservice"))
                .build();
    }

enter image description here

Community
  • 1
  • 1
Tapan Banker
  • 587
  • 1
  • 7
  • 10
5

U can also use springfox-swagger2 annotations. springfox.documentation.annotations.ApiIgnore

@ApiIgnore
public class ErrorController {

This would exclude that class from documentation.

Antony Mithun
  • 59
  • 1
  • 3
2

My problem was just that I forgot to annotate Docket api() method with @Bean.

1

This can be done by moving @Bean definition to main class (the one with @SpringBootApplication) and use its this.getClass().getPackageName() in basePackage():

@Bean
public Docket swagger() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage(this.getClass().getPackageName()))
            .paths(PathSelectors.any())
            .build()
            .useDefaultResponseMessages(false);
}
1

With Swagger.v3, we can use @Hidden. So:

import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyErrorController implements ErrorController {

  @RequestMapping("/error")
  @Hidden // Don't show in the Swagger doco
  @ResponseBody
  public void handleError(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // My handler code
  }
}
Craigo
  • 3,384
  • 30
  • 22
0

After trying a lot of solutions, nothing works for me. Finally I got to know the very basic thing i.e. make sure that the file in which you have defined your swagger configuration file and your main method file should be in the same package .

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
    .select()
    .apis(RequestHandlerSelectors.any())
    .paths(PathSelectors.any())
    .paths(Predicates.not(PathSelectors.regex("/error.*")))
    .build();
}

Please check this image

ASK
  • 1,136
  • 1
  • 10
  • 14
  • It will work fine even if they are in different packages. These is no such constraint like the one you have posted. – Rajkishan Swami Feb 15 '19 at 05:45
  • If your configuration bean files are in different packages, then you have to use **@componentScan("base-package")** annotation on your main method. – ASK Feb 15 '19 at 06:58
  • We also can use "import" annotation so that SwaggerConfig class can be imported in the main class. – ASK Apr 04 '19 at 03:39