Spring boot actuator can be used to monitor and manage the spring boot application. The predefined spring boot actuator endpoints provide a ready-to-use monitoring and management solution for the application. Spring boot actuator lets you create customized endpoints to track application-specific parameters and manipulate them using a custom endpoint url.
Spring boot customisable actuator endpoint enables the application to be managed and monitored using a customised set of monitoring parameters. In the spring boot application, the annotation @RestControllerEndpoint is used to create custom endpoints. Spring Boot’s latest version now allows you to define endpoints that are similar to the rest api implementation.
In this pose we will see about how to create a custom endpoint url in spring boot actuator and how to access the custom endpoint url.
Step 1 – Add spring boot Actuator dependency
The spring boot actuator has two dependents: spring-boot-starter-actuator and spring-boot-starter-web. The spring-boot-starter-actuator contains all of the actuator endpoints. The spring-boot-starter-web dependency exposes the endpoints in the web browser. The spring boot actuator will be used to enable the custom endpoint url, and the spring boot web dependency will be used to expose it on the web.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step 2 – add @RestControllerEndpoint
The custom endpoint is created using a class annotated with the @RestControllerEndpoint annotation. The @RestControllerEndpoint annotation enables the spring boot actuator to include class methods. Spring boot custom actuator endpoints and the GET method will be used to read the monitoring parameters. The id parameter of the annotation @RestControllerEndpoint specifies the endpoint’s name.
TestEndPointController.java
package com.yawintutor;
import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Component
@RestControllerEndpoint(id = "myendpoint")
public class TestEndPointController {
@GetMapping("/")
public @ResponseBody ResponseEntity<String> customGetEndPoint() {
return new ResponseEntity<String>("REST end point - GET", HttpStatus.OK);
}
@GetMapping("/{selector}")
public @ResponseBody ResponseEntity<String> customGetEndPoint(@PathVariable String selector) {
return new ResponseEntity<String>("REST end point - GET " + selector, HttpStatus.OK);
}
@PostMapping("/{selector}")
public @ResponseBody ResponseEntity<String> customPostEndPoint(@PathVariable String selector) {
return new ResponseEntity<String>("REST end point - POST " + selector, HttpStatus.OK);
}
@DeleteMapping("/{selector}")
public @ResponseBody ResponseEntity<String> customDeleteEndPoint(@PathVariable String selector) {
return new ResponseEntity<String>("REST end point - DELETE " + selector, HttpStatus.OK);
}
}
Step 3 – add configuration in application.properties
The custom endpoint must be enabled and available on the web in order to access the actuator endpoint url. Apply the following parameters to expose all of the endpoints available in the spring boot application. The configuration in the application.properties file exposes all endpoints. Another option is to expose the custom endpoint on its own in the web.
application.properties
management.endpoints.web.exposure.include=*
application.properties – to expose custom endpoint only
management.endpoints.web.exposure.include= myendpoint
Step 4 – How to access custom endpoints
The following are the custom endpoints that can be accessed. The GET method should contain the custom monitoring code. When the actuator custom endpoint url is called, the get method is invoked and sent to the custom endpoint.
custom url 1
http://localhost:8080/actuator/myendpoint
Output 1
REST end point - GET
custom url 2
http://localhost:8080/actuator/myendpoint/1
Output 2
REST end point - GET 1
Step 5 – How to post custom endpoint url
Using the HTTP POST method, the custom parameters can be sent to the custom endpoint url. Postman or the curl command can be used to call the post method. Curl is a simple command that can be found below. The url can be used to pass the argument, which assigns the value to the monitoring logic.
Curl command
curl -X POST http://localhost:8080/actuator/myendpoint/2
Output
REST end point - POST 2
Step 6 – How to delete custom endpoint url
The custom parameters can be supplied to the custom endpoint deletion url using the HTTP DELETE method. The delete method can be accessed via Postman or the curl command. Curl is a straightforward command that can be found in the list below. The url can be used to give an argument to the monitoring logic, which assigns the value to remove the values.
Curl command
curl -X DELETE http://localhost:8080/actuator/myendpoint/2
Output
REST end point - DELETE 2