配置 Swagger 以忽略 Spring Boot Actuator 端点

hot*_*oup 2 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我启动我的服务,然后转到http://localhost:8080/v2/api-docs(Swagger 的服务来源),然后将该 JSON 粘贴到jsonlint.com,我看到 Spring Boot 自动添加大约 40 个端点,我不希望Swagger 记录这些端点。像/trace端点、 and /health/envand/beans等东西。这些都是 Spring Boot Actuator 创建的东西,但我不想将其包含在我的公共 API 文档中。

有没有办法将 Swagger 配置为记录这些框架级端点?

Sau*_*Oza 5

RequestHandlerSelectors.any()
Run Code Online (Sandbox Code Playgroud)

将公开项目的所有端点。

除了上面的答案之外,您可以添加使用下面的代码,通过在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")); 
}
Run Code Online (Sandbox Code Playgroud)