Avoiding default basic-error-controller from swagger api [duplicate]
Asked Answered
C

7

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()
}
Costanzo answered 30/10, 2015 at 8:11 Comment(0)
C
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()
}
Costanzo answered 30/10, 2015 at 8:20 Comment(1)
Thanks! And I think it should be PathSelectors.regex("/error.*")Unicef
W
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

Wilden answered 12/4, 2017 at 8:1 Comment(1)
This worked for me. TAShetler
S
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();
}
Sow answered 19/6, 2018 at 8:5 Comment(0)
L
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

Lauren answered 7/12, 2017 at 14:48 Comment(0)
B
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"
    }
}
Blabber answered 25/3, 2019 at 13:16 Comment(1)
Thx. Bwt, seems not just related to specific controllersPulvinus
D
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();}
Desiccant answered 21/1, 2019 at 15:41 Comment(0)
L
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();
}
Libertylibia answered 23/3, 2019 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.