26

I'm using swagger2 in my spring boot project. It's working well, but I need to exclude the basic-error-controller from the api. Currently I'm using the following code using regex. It's working but is there any perfect way to do this.

CODE :

@Bean
public Docket demoApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.regex('(?!/error.*).*'))
            .build()
}
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188

7 Answers7

35

After searching in google I got the solution from one issue in GitHub, [question] How to exclude the basic-error-controller from being added to the swagger description?. It can be done using Predicates.not().

Code looks like as follows after using Predicates.not().

@Bean
public Docket demoApi() {
    return new Docket(DocumentationType.SWAGGER_2)//<3>
            .select()//<4>
            .apis(RequestHandlerSelectors.any())//<5>
            .paths(Predicates.not(PathSelectors.regex("/error.*")))//<6>, regex must be in double quotes.
            .build()
}
pvrforpranavvr
  • 2,708
  • 2
  • 24
  • 34
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
30

Lot's of time gone by, but if someone has same problem you could do it by providing selector for RestController:

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

Keeping in mind that your controllers are annotated with @RestController

4

I encountered the same problem. I did this.

java
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx"))
            .paths(PathSelectors.any())
            .build();
}
Geshode
  • 3,600
  • 6
  • 18
  • 32
Evan Knox Thomas
  • 592
  • 6
  • 12
3

The best way I found of limiting the endpoints that are displayed by the swagger documentation is doing this:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(paths())
            .build().apiInfo(metadata());
}

private Predicate<String> paths() {
    return or(
            regex("/firstContext.*"),
            regex("/secondContext.*"));
}

private ApiInfo metadata() {
    return new ApiInfoBuilder()
            .title("SomeTitle")
            .description("SomeDescription")
            .build();
}

So each endpoint that does not start with the paths() method contexts will not be rendered by swagger

3

If you are using a custom ErrorController just annotate it with

@ApiIgnore

or

@Api(hidden = true)

for example:

@Controller
@ApiIgnore
class MyErrorController : ErrorController {

    @RequestMapping("/error")
    fun handleError(request: HttpServletRequest): String {
        val status: String? = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)?.toString()
        val statusCode: Int? = status?.toInt()

        return when (statusCode) {
            HttpStatus.NOT_FOUND.value() -> return "error-404"
            HttpStatus.INTERNAL_SERVER_ERROR.value() -> return "error-500"
            else -> "error"
        }
    }

    override fun getErrorPath(): String {
        return "/error"
    }
}
Tobias
  • 7,282
  • 6
  • 63
  • 85
1

In my case when I make a method as @Bean than it will not show basic-error-controller.

If I remove @Bean it will show basic-error-controller in swagger-ui.

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage(CONTROLLER_PATH))
            .paths(regex("/.*")).build();}
1

What I think you should do is write some regex that matches all your API endpoints, if you are running microservices then that will probably be just one-word match if you don't then perhaps something that you put in the question makes more sense to me.

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.regex("/accounts.*"))
        .build();
}
Gerson Sosa
  • 366
  • 1
  • 4
  • 12