代码之家  ›  专栏  ›  技术社区  ›  hotmeatballsoup

配置swagger以忽略spring boot执行器端点

  •  1
  • hotmeatballsoup  · 技术社区  · 7 年前

    这里是spring boot restful web服务&swagger 2。我有以下的 @Configuration 为我的服务配置swagger的类设置:

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
        }
    }
    

    我开始服务然后去 http://localhost:8080/v2/api-docs (从中提供swagger)然后将该json粘贴到 jsonlint.com 我看到SpringBoot自动添加了大约40个端点, 不想要 大摇大摆地记录。像这样的事情 /trace 端点,和 /health ,请 /env /beans 等等,这些都是SpringBootActuator创建的,但我不希望包含在我的公共API文档中。

    是否有方法将swagger配置为 记录这些框架级端点?

    2 回复  |  直到 7 年前
        1
  •  1
  •   abdul    7 年前

    使用 any() 将通过swagger提供整个api的文档。使用 PathSelectors.ant() 限制你的终点。 有点像

    .paths(PathSelectors.ant("/finance/**")) 将只在 /finance/

        2
  •  1
  •   Saurabh Oza    6 年前
    RequestHandlerSelectors.any()
    

    将公开项目的所有终结点。

    除上述答案外,您还可以通过在 请求句柄选择器.basepackage() 方法。

    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.basePackage("com.assingment.scalableweb"))
          .paths(PathSelectors.any())                          
          .build()
          .apiInfo(metaData())
          .tags(new Tag("DifferenceController", "Operations related to the Json Data")); 
    }