java 在 swagger 中过滤 API 部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43935041/
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-03 07:50:10  来源:igfitidea点击:

Filtering for API parts in swagger

javaswaggerspringfox

提问by totee

I have a REST API and springfox swagger v2.6.1 included and working. But now, I would like to not always display all the controllers I have, because some of them are very technical and not intended for the average user, but I want to be able to choose what I show without having to recompile the code. There is this dropdown field on top of the page which says 'default (/v2/api-docs)' (or whatever you configured it to), with only this one entry. My hunch is that it should be possible to have multiple options there, and according to the option show certain controller classes or not.

我有一个 REST API 和 springfox swagger v2.6.1 并且正在工作。但是现在,我不想总是显示我拥有的所有控制器,因为其中一些非常技术性并且不适合普通用户,但是我希望能够选择我显示的内容而无需重新编译代码。页面顶部有一个下拉字段,上面写着“默认(/v2/api-docs)”(或您配置的任何内容),只有这个条目。我的预感是那里应该可以有多个选项,并根据选项显示或不显示某些控制器类。

As I don't really know how to upload images, I cannot provide screenshots. I hope that my question is clear anyway.

由于我真的不知道如何上传图片,我无法提供屏幕截图。无论如何,我希望我的问题很清楚。

The code that does the swagger in my project is the simplest possible:

在我的项目中大摇大摆的代码是最简单的:

@Bean
public Docket api() {

    return new Docket( DocumentationType.SWAGGER_2 )
            .select()
                .apis( RequestHandlerSelectors.any() )
                .paths( PathSelectors.any() )
                .build()
            .apiInfo( metadata() );
}

private ApiInfo metadata() {
    return new ApiInfoBuilder()
            .title( "My awesome ACS API" )
            .description( "All the requests that the server will respond to." )
            .version( "1.0.0" )
            .build();
}

I tried several approaches like adding some Properties, doing two .select() and selecting for different things, but I don't really seem to get anywhere close to what I hope to achieve.

我尝试了几种方法,例如添加一些属性、执行两个 .select() 并选择不同的东西,但我似乎并没有真正接近我希望实现的目标。

Thanks for any help!

谢谢你的帮助!

回答by pvpkiran

Some of the options I can think of

我能想到的一些选择

  1. You can add Authentication to different endpoints using SpringSecurity and make the endpoints not accessible at all(But will be visible in Swagger UI).

  2. The drop down you are mentioning on the top can be configured something like this

    @Bean
    public Docket orderApi() {
        // This will group the endpoints strting with /order.   
        // And it will be visible as a separate group in the drop down(In Swagger UI) 
        // with the name 'order' as specified in the groupName(..)
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("order")
                .apiInfo(metadata())
                .select()
                .paths(PathSelectors.ant("/order/**"))
                .build();
    }
    
    @Bean
    public Docket orderValidationApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("product")
                .apiInfo(metadata())
                .select()
                .paths(PathSelectors.ant("/product/**"))
                .build();
    }
    
  3. You can completely exclude the endpoint from being visible in Swagger UI with someting like this in your Docker configuration

       return new Docket( DocumentationType.SWAGGER_2 )
            .select()
                .apis( RequestHandlerSelectors.any() )
                .paths(PathSelectors.regex("(?!/error).+")).paths(PathSelectors.regex("(?!/product).+"))
                .build()
            .apiInfo( metadata() );  
    

    This will make all the endpoints which are not /error and /product available. you can filter out endpoints like this.

  1. 您可以使用 SpringSecurity 将身份验证添加到不同的端点,并使端点根本无法访问(但将在 Swagger UI 中可见)。

  2. 您在顶部提到的下拉菜单可以配置如下

    @Bean
    public Docket orderApi() {
        // This will group the endpoints strting with /order.   
        // And it will be visible as a separate group in the drop down(In Swagger UI) 
        // with the name 'order' as specified in the groupName(..)
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("order")
                .apiInfo(metadata())
                .select()
                .paths(PathSelectors.ant("/order/**"))
                .build();
    }
    
    @Bean
    public Docket orderValidationApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("product")
                .apiInfo(metadata())
                .select()
                .paths(PathSelectors.ant("/product/**"))
                .build();
    }
    
  3. 您可以使用 Docker 配置中的类似内容完全排除端点在 Swagger UI 中的可见性

       return new Docket( DocumentationType.SWAGGER_2 )
            .select()
                .apis( RequestHandlerSelectors.any() )
                .paths(PathSelectors.regex("(?!/error).+")).paths(PathSelectors.regex("(?!/product).+"))
                .build()
            .apiInfo( metadata() );  
    

    这将使所有不是 /error 和 /product 的端点可用。您可以像这样过滤掉端点。