簡體   English   中英

過濾 swagger 中的 API 零件

[英]Filtering for API parts in swagger

我有一個 REST API 和 springfox swagger v2.6.1 包括和工作。 但是現在,我不想總是顯示我擁有的所有控制器,因為其中一些非常技術性並且不適合普通用戶,但我希望能夠選擇我顯示的內容而無需重新編譯代碼。 頁面頂部有一個下拉字段,上面寫着“默認 (/v2/api-docs)”(或您配置的任何內容),只有一個條目。 我的直覺是那里應該有多個選項,並且根據選項顯示某些 controller 類或不。

由於我真的不知道如何上傳圖片,我無法提供截圖。 無論如何,我希望我的問題很清楚。

在我的項目中執行 swagger 的代碼是最簡單的:

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

我嘗試了幾種方法,例如添加一些屬性、執行 two.select() 和選擇不同的東西,但我似乎並沒有真正接近我希望實現的目標。

謝謝你的幫助!

我能想到的一些選擇

  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. 您可以完全排除端點在 Swagger UI 中的可見性,在您的 Docker 配置中使用類似的內容

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

    這將使所有不是 /error 和 /product 的端點可用。 您可以像這樣過濾掉端點。

您還可以提供包名

@Bean
public Docket api() {

    return new Docket( DocumentationType.SWAGGER_2 )
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.hello.world.controller.user"))
            .paths( PathSelectors.any() )
            .build()
            .apiInfo( metadata() );
}

或者您可以為您的 API 類或 API 方法使用自定義注釋,如下所示:

@Bean
public Docket api() {

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



@Bean
public Docket api() {

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

@Hidden 注釋對我有用。

@GetMapping("/ping")
@Hidden
public String ping() {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM