In the last article, we discussed Spring Boot Actuator.In this post, we will discuss how to create a custom endpoint in Spring Boot Actuator.
Introduction
The actuator provides production-ready features for Spring Boot application.It will help us to check and manage our application in the production environment.There are a number of endpoints provided by Spring Boot however we may want to create a custom endpoint using Spring Boot Actuator.
Spring Boot 2.x introduced a number of changed including endpoint configurations.In this post, we will create a custom endpoint with Spring Boot 1.x and 2.x.
1. Custom Endpoint using Spring Boot 1.x
To implement a new endpoint for our application using Spring Boot 1.x, we should expose the instance of the custom endpoint class as a bean.We need to implement Endpoint<T>
interface.
@Component
public class CustomEndpoint implements Endpoint {
@Override
public String getId() {
return "custom-endpoint";
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public boolean isSensitive() {
return false;
}
@Override
public String invoke() {
return "This is a custom end point for demo purpose";
}
}
To access our custom endpoint, use the id field (for our example, it is “custom-endpoint“).
{
This is a custom end point for demo purpose
}
2. Custom Endpoint with Spring Boot 2.x
Spring Boot 2 provides an easy way to create custom endpoints.Spring Boot 2.x introduced @Endpoint
annotation.Spring Boot automatically expose endpoints with@Endpoint
, @WebEndpoint
, or @WebEndpointExtension
over HTTP using Jersey, Spring MVC, or Spring WebFlux.
Spring Boot 2.x Actuator support CURD model, it supports read, writes and delete operation with the endpoints.The @Endpoint
annotation can be used in combination with @ReadOperation,@WriteOperation
and @DeleteOperation
to develop endpoints.
2.1 Creating Custom Endpoint
We are creating a custom health endpoint, this endpoint will provide a custom information to the client.
Data Model
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class CustomHealth {
private Map<String, Object> healthDetails;
@JsonAnyGetter
public Map<String, Object> getHealthDetails() {
return this.healthDetails;
}
}
Custom Health endpoint.
@Component
@Endpoint(id="custom-health")
public class CustomHealthEndPoint {
@ReadOperation
public CustomHealth health() {
Map<String, Object> details = new LinkedHashMap<>();
details.put("CustomHealthStatus", "Everything looks good");
CustomHealth health = new CustomHealth();
health.setHealthDetails(details);
return health;
}
@ReadOperation
public String customEndPointByName(@Selector String name) {
return "custom-end-point";
}
@WriteOperation
public void writeOperation(@Selector String name) {
//perform write operation
}
@DeleteOperation
public void deleteOperation(@Selector String name){
//delete operation
}
}
[pullquote align=”normal”]Keep an eye on #11107 while naming your endpoint. There is an interesting issue if we name our endpoint in camel case [/pullquote]
- The Id property of the
@Endpoint
annotation determines the mapping of our endpoint (in our example it is /custom-endpoint). @ReadOperation
– HTTP Get method.@WriteOperation
– POST method.@DeleteOperation
– HTTP DELETE operation.
To access our custom endpoint, use <em>http://host:port/actuator<em>/custom-health</em></em>
to check the output.
{
"CustomHealthStatus":"Everything looks good"
}
2.2 Controller Endpoints
Spring Boot Actuator provides an alternate way to create custom endpoints that are only exposed by Spring MVC or Spring WebFlux.Use @ControllerEndpoint
and @RestControllerEndpoint
for this.While using this approach, we should use standard Spring MVC annotations like @RequestMapping
and @GetMapping
, with the endpoint’s ID being used as a prefix for the path.
@Component
@RestControllerEndpoint(id = "rest-end-point")
public class RestCustomEndPoint {
@GetMapping("/custom")
public @ResponseBody ResponseEntity customEndPoint(){
return new ResponseEntity<>("REST end point", HttpStatus.OK);
}
}
Use <em>http://host:port/actuator/rest-end-point/custom</em>
to access this custom endpoint.
Summary
Spring Boot Actuator provides a number of ready to use powerful features.In this post, we learned to create a custom endpoint in Spring Boot Actuator.We covered creating custom endpoints using both Spring Boot 1.x and 2.x.
good article.
I would like to add something more on top of this customization, we can also customize the url by removing actuator word form it by using following property : management.endpoints.web.base-path=/any-thing-you-want and your new url will become http://host:port/context-path/any-thing-you-want/rest-end-point/custom
Thanks Irfan for this additional information, I will update the post with this additional information
Also i think is springbooth 2.3 you cannot name endpoint with “-“
I don’t see any such change, can you point me the release note indicating such changes?