迅速地过滤API零件

tot*_*tee 3 java swagger springfox

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

由于我真的不知道如何上传图像,因此无法提供屏幕截图。我希望我的问题仍然清楚。

在我的项目中大张旗鼓的代码是最简单的:

@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();
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助!

pvp*_*ran 8

我能想到的一些选择

  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();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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() );  
    
    Run Code Online (Sandbox Code Playgroud)

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