配置Swagger以忽略Spring Boot Actuator端点

时间:2018-07-06 11:23:30

标签: spring-boot swagger-2.0 spring-boot-actuator

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中,我发现Spring Boot会自动添加大约40个端点不想挥挥手记录下来。诸如/trace端点,/health/env/beans之类的东西。这些都是Spring Boot Actuator创建的所有东西,但我不想包含在公共环境中API文档。

是否可以将Swagger配置为记录这些框架级端点?

2 个答案:

答案 0 :(得分:1)

使用any()将通过Swagger提供整个API的文档。使用PathSelectors.ant()限制端点。

.paths(PathSelectors.ant("/finance/**"))仅显示/finance/下的端点

答案 1 :(得分:1)

RequestHandlerSelectors.any()

将公开您项目的所有端点。

在以上答案的顶部,您可以通过在 RequestHandlerSelectors.basePackage()方法中提供基本包名称来添加使用下面的代码来限制API的显示。

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