java 从 swagger api 避免默认的基本错误控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33431343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:39:05  来源:igfitidea点击:

Avoiding default basic-error-controller from swagger api

javaspringspring-bootswagger-2.0spring-restcontroller

提问by Pranav C Balan

I'm using swagger2in my spring boot project. It's working well, but I need to exclude the basic-error-controllerfrom the api. Currently I'm using the following code using regex. It's working but is there any perfect way to do this.

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

CODE :

代码 :

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

回答by Pranav C Balan

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

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

Code looks like as follows after using 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()
}

回答by Vytautas Alkimavi?ius

Lot's of time gone by, but if someone has same problem you could do it by providing selector for RestController:

很多时间过去了,但是如果有人遇到同样的问题,您可以通过为 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

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

回答by Tobias

If you are using a custom ErrorControllerjust annotate it with

如果您使用的是自定义,ErrorController只需注释它

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

回答by Henrique Schmitt

The best way I found of limiting the endpoints that are displayed by the swagger documentation is doing this:

我发现限制 swagger 文档显示的端点的最佳方法是这样做:

@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

因此,每个不以 path() 方法上下文开头的端点都不会被 swagger 渲染

回答by Evan

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

回答by Gerson Sosa

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.

我认为您应该做的是编写一些与您的所有 API 端点匹配的正则表达式,如果您正在运行微服务,那么这可能只是一个词匹配,如果您不这样做,那么您在问题中提出的内容可能更有意义我。

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.regex("/accounts.*"))
        .build();
}

回答by Madhusudan Joshi

In my case when I make a method as @Bean than it will not show basic-error-controller.

在我的情况下,当我将方法设为 @Bean 时,它不会显示基本错误控制器。

If I remove @Bean it will show basic-error-controller in swagger-ui.

如果我删除@Bean,它将在 swagger-ui 中显示 basic-error-controller。

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