Remove Basic Error Controller In SpringFox Swagger

2020-01-29 08:14发布

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

Picture:

enter image description here

7条回答
Root(大扎)
2楼-- · 2020-01-29 08:50

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.

查看更多
狗以群分
3楼-- · 2020-01-29 08:52
  • It can be done using Predicates.not() .

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .paths(Predicates.not(PathSelectors.regex("/error.*")))
            .build();
    }
    
查看更多
ゆ 、 Hurt°
4楼-- · 2020-01-29 09:00

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

@ApiIgnore
public class ErrorController {

This would exclude that class from documentation.

查看更多
看我几分像从前
5楼-- · 2020-01-29 09:02

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

查看更多
叛逆
6楼-- · 2020-01-29 09:07

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

查看更多
不美不萌又怎样
7楼-- · 2020-01-29 09:11

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" ) )
        ...
查看更多
登录 后发表回答