Custom Endpoint in Spring Boot Actuator

 

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.

4 thoughts on “Custom Endpoint in Spring Boot Actuator”

Comments are closed.