Custom Endpoint in SpringBoot Actuator

Technology: SpringBoot Actuator provides production ready features for springBoot application. It will help us to check and manage the application in production environment. SpringBoot Actuator provides number of endpoints which are used known the state of the system at runtime.

SpringBoot Actuator provides the way to implement the custom endpoints including the configuration.
SpringBoot 2 provides an easier way to create custom endpoint using @Endpoint annotation. @Endpoint annotated classes are automatically exposes by spring boot, Spring MVC and Spring WebFlux.

SpringBoot 2 Actuator support the CRUD model, It supports the read,write, delete operations with the support of @ReadOperation, @WriteOperation, and @DeleteOperation. @Endpoint annotation will be combined with above operations to provides Rest/JMX endpoint.

Project Setup:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Creating Custom Endpoint:

We can create Custom endpoint in 2 ways, using @Endpoint, and using @RestControllerEndpoint annotation.

First we will learn how to create Custom Endpoint using @Endpoint annotation.
First create sample POJO class for returning the JSON object.

public class Sample implements Serializable {
	private static final long serialVersionUID = 1L;

	private String title;
	private String message;

	public Sample(String title, String message) {
		super();
		this.title = title;
		this.message = message;
	}

	public String getTitle() {
		return title;
	}

	public String getMessage() {
		return message;
	}
}

Next we will create class with annotated with @Endpoint and write some methods annotated with @ReadOperation, @WriteOperation and @DeleteOperation.

@Component
@Endpoint(enableByDefault=true, id="sample")
public class SampleEndpoint {
	
	@ReadOperation
	public Sample getSampleEndpoint()
	{
		return new Sample("sravan","kumar");
	}
	@ReadOperation
	public Sample getUserSample(@Selector String title, @Selector String message)
	{
		return new Sample(title,message);
	}
	@WriteOperation
	public void writeSample(@Selector String title, @Selector String message)
	{
		System.out.println("write operation with title:"+title +" and message "+message);
	}
	@DeleteOperation
	public void deleteSample(@Selector String title)
	{
		System.out.println("delete operation with title:"+title);
	}
}

And Enable the Spring Boot Actuator endpoints using below property in application.properties file.
management.endpoints.web.exposure.include=*

Spring Boot helps you accelerate and facilitate Java application development.

Now, Run the spring boot application, and Navigate to http://localhost:8080/actuator, and verify our new endpoints in response.

"sample": {
      "href": "http://localhost:8080/actuator/sample",
      "templated": false
    },
    "sample-title-message": {
      "href": "http://localhost:8080/actuator/sample/{title}/{message}",
      "templated": true
    },
    "sample-title": {
      "href": "http://localhost:8080/actuator/sample/{title}",
      "templated": true
},

Here we used @Selector annotation, it will append to url as Path variable.
Navigate to http://localhost:8080/actuator/sample

We will observe the response as
“{“title”:”sravan”,”message”:”kumar”} “.

The above request is executed the first method with zero method arguments.

Now Navigate to http://localhost:8080/actuator/sample/kumar/sravan we can see the response as “{“title”:”kumar”,”message”:”sravan”}”, this url is invoked the second with 2 method arguments annotated with @Selector annotation and @ReadOperation.

Now execute the below request

curl  -X POST http://localhost:8080/actuator/sample/sravan/kumar

Which will log the message with “write operation with title:sravan and message kumar”, means it was invoked the method annotated with @WriteOperation.

curl  -X DELETE http://localhost:8080/actuator/sample/sravan

It will execute the method annotated with @DeleteOperation, and log the message “delete operation with title:sravan”.

Now we will learn how to create Endpoints with @RestControllerEndpoint annotation.

Create a class annotated with @RestControllerEndpoint and @Component and add requestmapping similar to controller method annotation with @GetMapping, @PostMapping etc..

@Component
@RestControllerEndpoint(id="sampleRest")
public class SmapleRestEndPoint {

	@GetMapping("sample")
	public Sample getContollerEndpoint()
	{
		return new Sample("rest","endpoint");
	}
}

Now restart the application, now we can observe the samplerest endpoint in the response.

Now Navigate to http://localhost:8080/actuator/sampleRest/sample we can see the response as “{“title”:”rest”,”message”:”endpoint”}”, we can observe that it is invoking the getControllerEndpoint method and returning the response.

Conclusion: We leaned above the Spring Boot Actuator, and How to create custom endpoints using SpringBoot @Endpoint and also using @RestControllerEndpoint.

Leave a Reply

Your email address will not be published.




*

code