Avoiding default basic-error-controller from swagger api

<>

This question already has an answer here:

Answers


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

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


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"
    }
}

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


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

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

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

Need Your Help

Math in reStructuredText with LaTeX

math latex markdown restructuredtext gedit

I would like to use a lightweight markup language to take notes in my college classes.

Pros & Cons of TRUNCATE vs DELETE FROM

sql-server sql-server-2008 tsql sql-delete truncate

Could someone give me a quick overview of the pros and cons of using the following two statements: