Avoid default base-controller-error from swagger api

I am using swagger2 in my spring boot project. It works well, but I need to exclude basic-error-controller from the api. I am currently using the following code with regex. It works, 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() } 
+13
source share
5 answers

After searching on Google, I got a solution to one problem on GitHub, [question] How to exclude the main-erroneous controller from adding to the swagger description? , This can be done using Predicates.not() .

The code is 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() } 
+22
source

A lot of time has passed, but if someone has the same problem, you can do this by providing the RestController selector:

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

Keeping in mind that your controllers are annotated with @RestController

+11
source

The best way I've found to limit the endpoints displayed by the swagger documentation is as follows:

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

Thus, each endpoint that does not start with the contexts of the paths () method will not be displayed using swagger

+1
source

I ran into the same problem. I have done it.

 java @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx")) .paths(PathSelectors.any()) .build(); } 
0
source

In my case, when I create a method like @Bean, it will not show basic-error-controller.

If I remove @Bean, basic-error-controller will be shown in swagger-ui.

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

All Articles