Spring Boot Actuator / Swagger

ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2)
  .useDefaultResponseMessages(false)
  .apiInfo(apiInfo())
  .securitySchemes(securitySchemes())
  .select()
  .apis(RequestHandlerSelectors.any())
  .paths(Predicates.not(PathSelectors.regex("/actuator.*")))
  .build();

Hi, You can exclude path on regex and you can chain them.


Update: 26-04-2017, updated implemention. Credits to Andy Brown for the tip.

Due to our coding convention, we don't have a specific prefix for our endpoints, so I was looking for a solution to exclude the actuator endpoints, rather than to include my own paths.

I've came up with the following config to only exclude the actuator endpoints. This way, I don't have to update the config once I add new endpoints nor do I have to prefix my own endpoints to distinguish them from the actuator endpoints.

/**
 * This enables swagger. See http://localhost:8080/v2/api-docs for the swagger.json output!
 * @param actuatorEndpointHandlerMapping this endpoint handler mapping contains all the endpoints provided by the
 * spring actuator. We will iterate over all the endpoints and exclude them from the swagger documentation.
 * @return the docket.
 */
@Autowired
@Bean
public Docket swaggerSpringMvcPlugin(final EndpointHandlerMapping actuatorEndpointHandlerMapping) {
    ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2)
            .useDefaultResponseMessages(false)
            .apiInfo(apiInfo())
            .securitySchemes(securitySchemes())
            .select();

    // Ignore the spring-boot-actuator endpoints:
    Set<MvcEndpoint> endpoints = actuatorEndpointHandlerMapping.getEndpoints();
    endpoints.forEach(endpoint -> {
        String path = endpoint.getPath();
        log.debug("excluded path for swagger {}", path);
        builder.paths(Predicates.not(PathSelectors.regex(path + ".*")));
    });

    return builder.build();
}

You can configure in Swagger which paths you want to be added to the documentation :

@Bean
public Docket appApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            ...
}

will display all available endpoints.

.paths(PathSelectors.any("/mypath/**")) will limit only to endpoints exposed in mypath.


In order to display spring-boot-actuator endpoints in springdoc-openapi, simply add the following property:

springdoc.show-actuator=true