Spring Boot Actuator / Swagger

I am working on a Spring application to download, and I am using Swagger for documentation.

I have an additional Spring Boot Actuator in my application, but now I want to add new services created by the drive (/ health / metrics ..) in my swagger documentation.

I cannot find how to configure Actuator and Swagger.

+7
source share
4 answers

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

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

displays all available endpoints.

.paths(PathSelectors.any("/mypath/**")) will be limited only to endpoints, mypath. in mypath.

+8
source

Update: 04/26/2017, updated implementation. Andy Brown credits for a clue.

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

I came up with the following configuration to exclude only the endpoints of the actuator. Thus, I do not need to update the configuration when I add new endpoints, and I do not need to prefix my own endpoints to distinguish them from the endpoints of the actuator.

 /** * 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(); } 
+1
source
 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 the path in the regex, and you can link them.

0
source

Move the drive endpoints to the context path through the application.properties file.

 management.context-path=/manage 

Then you can exclude this path from swagger

 @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(Predicates.not(PathSelectors.regex("/manage.*"))) .build(); } 

You can also exclude the error controller

 @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(Predicates.not(PathSelectors.regex("(/manage.*|/error)"))) .build(); } 
0
source

Source: https://habr.com/ru/post/1235790/


All Articles