Spring Boot: Custom Actuator EndPoints Creation


In the previous post , Spring Boot Actuator : : Monitor Health of Application you have learn about the Spring Boot actuator enabling and access health monitoring parameters through Spring Boot Actuator provided endpoints (default).

In this post, You will learn about to create custom Spring Boot Actuator end points. Sometimes these custom actuator endpoints required to get some specific information or customization of information instead of providing all detailed application health related information.

You have to follow these steps in Spring Boot application to create custom actuator endpoints.

Step 1:

Create or import any Spring Boot REST application. For Example using for User related Rest service (CRUD).

Step 2:

Enable the actuator in Spring Boot application by adding actuator starter in application pom.xml

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

Step 3:

By default actuator allows external access for endpoints /actuator /health and /info, to access others custom actuator endpoints. you have add this property in your application.properties file.

management.endpoints.web.exposure.include=* 

Now, start your application and see the actuator is working fine or not by trying below url. If your server running on port 8090.

http://localhost:8090/actuator/health

{"status":"UP"}

UP status shows your application is running fine.

See More : Spring Boot Actuator : : Monitor Health of Application

Step 4:

Now to create custom actuator endpoints , you can create a class MyUserEndPoints in package com.facingIssuesOnIT.actuator as below:

To create a new endpoint you have to create a class and annotate it with @Component and @Endpoint annotation. The @Endpoint annotation has a parameter id (Ex: users) which determines the URL path of endpoint. This class contains the methods which returns the response of the endpoint. 

package com.FacingIssuesOnIT.actuators;

impert java.util.List;
impert java.util.Optional;

impert org.springframework.beans.factory.annotation.Autowired;
impert org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
impert org.springframework.boot.actuate.endpoint.annotation.Endpoint;
impert org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
impert org.springframework.boot.actuate.endpoint.annotation.Selector;
impert org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
impert org.springframework.core.env.Environment;
impert org.springframework.stereotype.Component;

impert com.FacingIssuesOnIT.model.User;
impert com.FacingIssuesOnIT.repository.UserRepository;

at2Component
at2Endpoint(id = "users")
public class MyUserEndpoints {
	
		at2Autowired
		private UserRepository userRepository;
		
		at2Autowired
		private Environment environment;
		
		at2ReadOperation
		public List<User> getAllUsers(){
			List<User> users =  userRepository.findAll();			
			return users;
		}
		
		at2WriteOperation
	    public String updateUser(Integer userId, String emailId) throws Exception {
		Optional<User> optional = userRepository.findById(userId);
			User user = optional.orElseThrow(() -> new Exception("Service.USER_NOT_FOUND"));
			user.setEmail(emailId);			
		return environment.getProperty("API.UPDATE_SUCCESS");
	}
		
		at2DeleteOperation
		public String deleteUser(at2Selector Integer userId) {
			userRepository.deleteById(userId);
			return environment.getProperty("API.DELETE_SUCCESS");
		}		
} 

In above class,

  • The value of id parameter of @Endpoint is usersSo this endpoint is accessible by URL /actuator/users.
  • a method defined with @ReadOperation which will be mapped to HTTP GET method and automatically be exposed over HTTP.
  • a method defined with @WriteOperation which will be mapped to HTTP POST method and automatically be exposed over HTTP. The methods that are annotated with @WriteOperation can take parameters in JSON format alone.
  • a method defined with @DeleteOperation which will be mapped to HTTP DELETE method and automatically be exposed over HTTP.
  • technology-specific endpoints defined with @WebEndpoint/@JmxEndpoint. For ex, @WebEndpoint is exposed over HTTP only.

Step 5:

Restart the application and try with different custom actuator end points as below:

GET Method : http://localhost:8090/actuator/users

Response: Existing users list

Spring Boot Custom Actuator Endpoints : Get Method

POST Method : http://localhost:8090/actuator/users

Request Body

Spring Boot Actuator Endpoints : Post Method

It will create new user sunny with Id 345

Delete Method: http://localhost:8090/actuator/users/3

Spring Boot Actuator Endpoints : Delete Method

Here you have seen the custom actuator endpoints urls and response through the postman.

Conclusion

In this post you have learn about the below points:

  • Spring Boot Actuator configuration and enabling
  • Spring Boot custom actuator endpoints creation steps.
  • Spring Boot custom actuator endpoints access through Postman.

Let me know your thought on this post.

Happy Learning !!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s