java - 避免来自 swagger api 的默认基本错误 Controller

我在我的 Spring Boot 项目中使用 swagger2。它运行良好,但我需要从 api 中排除 basic-error-controller 。目前我使用正则表达式使用以下代码。它正在工作,但有什么完美的方法可以做到这一点。

代码:

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

最佳答案

在谷歌搜索后,我从 GitHub 的一个问题中得到了解决方案, [question] How to exclude the basic-error-controller from being added to the swagger description? 。可以使用 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()
}

https://stackoverflow.com/questions/33431343/

相关文章:

java - AspectJ 表达式在切入点错误中给出正式的未绑定(bind)

spring - 配置多个数据源后无法设置JPA命名策略(Spring 1.4.1/Hibernat

java - 无法将 boolean 值设置为 null

java - 如何强制 Spring 容器不返回 bean 的单例实例?

java - @ControllerAdvice 在处理异常方面比 @ExceptionHandle

java - 从另一个异步方法调用的 Spring 异步方法

java - 在 Spring Boot 中读取环境变量

java - 在 Spring Boot 应用程序中安排任务的最佳方法是什么

java - 为什么来自 Spring 的 BCryptPasswordEncoder 为相同的输入

java - 比较 SpEL 中的枚举