Spring boot

Spring boot actuator custom endpoint example

Custom endpoint implementation

If you add a @Bean annotated with @Endpoint, any methods annotated with @ReadOperation, @WriteOperation, or @DeleteOperation are automatically exposed over JMX and, in a web application, over HTTP as well.

Spring Boot Custom Endpoints can be exposed over HTTP using Jersey, Spring MVC, Spring Rest or Spring WebFlux.
If both Jersey and Spring MVC are available, Spring MVC will be used.

Below example to uses Spring rest for implementation of custom endpoint

Creating endpoint using @Endpoint (CustomHealthEndPoint.java)

Use @Endpoint annotation to expose the class to the actuator that provides information about the running application. Spring Boot Endpoints can be exposed over a variety of technologies including JMX and HTTP.

Most @Endpoint classes will declare one or more @ReadOperation, @WriteOperation, @DeleteOperation annotated methods which will be automatically adapted to the exposing technology (JMX, Spring MVC, Spring WebFlux, Jersey etc.)

package com.candidjava.SpringBootCustomEndPointActuator.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@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;
 }
}

CustomHealth.java

package com.candidjava.SpringBootCustomEndPointActuator.controller;

import java.util.Map;

public class CustomHealth {

 private Map<String,Object> healthDetails;

 public void setHealthDetails(Map<String,Object> healthDetails) {
  this.healthDetails = healthDetails;
 }

 public Map<String,Object> getHealthDetails() {
  return this.healthDetails;
 }
}

Configure custom endpoint application.properties

management.endpoints.web.exposure.include = custom-health,health

Adding actuator dependency (pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.candidjava.springboot</groupId>
	<artifactId>SpringBoot_CustomEndPoint_Actuator</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot_CustomEndPoint_Actuator Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
		<finalName>SpringBoot_CustomEndPoint_Actuator</finalName>
	</build>
</project>

Sample Controller (HelloWorldController.java)

package com.candidjava.SpringBootCustomEndPointActuator.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

 @GetMapping(value = "/")
 public String getMessage() {
  return "Hello World";
 }

}

Actuator security (SpringSecurityConfig.java)

Configure username and password for accessing management port using spring boot in-memory authentication.

package com.candidjava.SpringBootCustomEndPointActuator.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  auth.inMemoryAuthentication().withUser("admin").password("{noop}admin").roles("ADMIN");
  auth.inMemoryAuthentication().withUser("act").password("{noop}act").roles("ACTUATOR");

 }
}

Launch Spring application (SpringBootCustomEndPointActuatorApplication.java)

package com.candidjava.SpringBootCustomEndPointActuator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootCustomEndPointActuatorApplication {

 public static void main(String[] args) {
  SpringApplication.run(SpringBootCustomEndPointActuatorApplication.class, args);
 }

}

Download

Download source code from my github account Click here