This article explains how to map custom Spring Boot Actuator endpoints to custom path in Spring Boot 1.x.x and 2.x.x
Spring Boot has been getting quite popular for Microservices development for good reasons. One of the reasons is to provide production ready endpoints for service management such as endpoints for metrics, service info, health check, env variables, heap dump to name some of those.
In this article, we will talk about how to expose these Spring Boot endpoints to a custom path.
Enabling and Exposing Spring Boot Endpoints
Spring Boot exposes these endpoints with the help of Actuator library. Hence, first thing is to ensure that following dependency is present in your service pom.xml file. If you are using spring-boot-starter-parent as your project's parent, you wouldn't need to specify actuator version as it will automatically be managed by parent for you.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Starting Spring Boot 2.x.x, only info and health are exposed by default while others need to be disabled by adding following to your service properties file -
# takes comma separated names of endpoints management.endpoints.web.exposure.include=metrics,heapdump # To expose all endpoints, use * as follows management.endpoints.web.exposure.include=*
Exposing Boot Endpoints on a custom path
Once you have enabled Spring Boot endpoints, management endpoints will be available at http://hostname:port/<servletpath>/<endpointspath>/<endpointname> in Spring Boot 2.x.x and http://hostname:port/<servletpath>/<endpointname> in Spring Boot 1.x.x.
<servletpath> points to / by default and can be changed using properties server.servlet.path and server.servlet-path in Spring boot 2.x.x and 1.x.x respectively.
<endpointspath> is set to /actuator by default and is only applicable in Spring Boot 2.x.x. You can modify it using property management.endpoints.web.base-path.
E.g. if you want to expose all the endpoints on http://hostname:port/app/management/<endpointname>, you can have it configued using any of following approaches -
Spring Boot 2.x.x
# Approach 1 server.servlet.path=/app management.endpoints.web.base-path=/management # Approach 2 server.servlet.path=/ management.endpoints.web.base-path=/app/management
Spring Boot 1.x.x
Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.