Adding Actuator as a groupedOpenApi

3.6k Views Asked by At

Could we add Actuator endpoints as a groupedOpenApi that will be gourped separately ?

ex :

@bean
public GroupedOpenApi actuatorApi() {
return GroupedOpenApi.builder().setGroup("Actuator")
.packagesToScan("org.springframework.boot.actuate")
.pathsToMatch("/actuator/**")
.build();
}

Thanks

2

There are 2 best solutions below

2
On BEST ANSWER

First you need to enable actuator on the swagger-ui:

springdoc.show-actuator=true

You just need to declare GroupedOpenApi bean:

@Bean
 public GroupedOpenApi actuatorApi(){
     String[] paths = {"/actuator/**"};
     return GroupedOpenApi.builder()
             .setGroup("groups")
             .pathsToMatch(paths)
             .build();
 } 

Here are the steps to add actuator:

https://github.com/springdoc/springdoc-openapi-demos/commit/aa6bcf1f0312c8fc36f94aeb4653718b36c308f6

0
On

The solution provided by @brianbro mostly works but there is an issue where some endpoints that use parameters in URL, e.g. /actuator/metrics/{requiredMetricName} due to way spring boot handles the actuator endpoints (single handler method, no @PathParam) the PathParam fields don't show in Swagger. See Spring boot actuator endpoint parameters issue.

Example of what shows in Swagger without resolving the issue: enter image description here Example of what shows in Swagger after resolving the issue: enter image description here How to fix this is discussed in Adding Actuator as a groupedOpenApi but some of the links are broken. The key issue is there is an ActuatorOpenApiCustomiser class that is used to fix the PathParams for Actuator, and that class is not being called when Actuator resides within a GroupedOpenApi.

Full solution (working with springdoc:1.6.9)...

First you need to enable actuator on the swagger-ui:

springdoc.show-actuator=true

Declare GroupedOpenApi bean using code (you can't use the springdoc.group-configs[0] properties because you need to add a OpenApiCustomiser to the group):

@Bean
 public GroupedOpenApi actuatorApi(OpenApiCustomiser actuatorOpenApiCustomiser){
     String[] paths = {"/actuator/**"};
     return GroupedOpenApi.builder()
             .setGroup("Actuator")
             .pathsToMatch(paths)
             .addOpenApiCustomiser(actuatorOpenApiCustomiser)
             .build();
 }

Addtional Sample from SpringDoc