Remove Basic Error Controller In SpringFox SwaggerUI

Swagger UiSwagger 2.0Springfox

Swagger Ui Problem Overview


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

Picture:

enter image description here

Swagger Ui Solutions


Solution 1 - Swagger Ui

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" ) )
        ...

Solution 2 - Swagger Ui

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.

Solution 3 - Swagger Ui

  • 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();
     }
    

Solution 4 - Swagger Ui

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

Solution 5 - Swagger Ui

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

@ApiIgnore
public class ErrorController {

This would exclude that class from documentation.

Solution 6 - Swagger Ui

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

Solution 7 - Swagger Ui

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);
}

Solution 8 - Swagger Ui

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajkishan SwamiView Question on Stackoverflow
Solution 1 - Swagger UiCarsten ZeitzView Answer on Stackoverflow
Solution 2 - Swagger UibladekpView Answer on Stackoverflow
Solution 3 - Swagger UipvrforpranavvrView Answer on Stackoverflow
Solution 4 - Swagger UiTapan BankerView Answer on Stackoverflow
Solution 5 - Swagger UiAntony MithunView Answer on Stackoverflow
Solution 6 - Swagger UiVinicius RodriguesView Answer on Stackoverflow
Solution 7 - Swagger UiRamirezRodriguezView Answer on Stackoverflow
Solution 8 - Swagger UiASKView Answer on Stackoverflow