避免使用swagger api的默认基本错误控制器

Pra*_*lan 18 java spring spring-boot swagger-2.0 spring-restcontroller

我在我的春季启动项目中使用了swagger2.它工作得很好,但我需要basic-error-controller从api中排除它.目前我使用正则表达式使用以下代码.它正在运作,但有任何完美的方法来做到这一点.

代码:

@Bean
public Docket demoApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.regex('(?!/error.*).*'))
            .build()
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*lan 28

在谷歌搜索后,我从GitHub中的一个问题得到了解决方案,[问题]如何排除基本错误控制器被添加到招摇说明中?.它可以使用Predicates.not().

使用后代码如下所示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()
}
Run Code Online (Sandbox Code Playgroud)


Vyt*_*ius 19

很多时间过去了,但如果有人有同样的问题,你可以通过为RestController提供选择器来做到这一点:

new Docket(SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build();
Run Code Online (Sandbox Code Playgroud)

请记住,您的控制器使用@RestController进行注释