0
0
Spring Bootframework~5 mins

Health endpoint customization in Spring Boot

Choose your learning style9 modes available
Introduction

The health endpoint shows if your app is working well. Customizing it helps you add extra checks or change what info it shows.

You want to check if your database is connected.
You need to see if an external service your app uses is up.
You want to hide some details from the health report for security.
You want to add a custom message or status to the health check.
Syntax
Spring Boot
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // Your custom health check logic
        boolean serviceUp = checkService();
        if (serviceUp) {
            return Health.up().withDetail("service", "Available").build();
        } else {
            return Health.down().withDetail("service", "Not Available").build();
        }
    }

    private boolean checkService() {
        // Check your service here
        return true;
    }
}

Implement HealthIndicator and override health() to add custom checks.

Use Health.up() or Health.down() to set status and add details with withDetail().

Examples
This example adds a database check to the health endpoint.
Spring Boot
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class DatabaseHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        boolean dbUp = checkDatabase();
        if (dbUp) {
            return Health.up().withDetail("database", "Connected").build();
        } else {
            return Health.down().withDetail("database", "Disconnected").build();
        }
    }

    private boolean checkDatabase() {
        // Simulate DB check
        return true;
    }
}
This example adds a simple custom message to the health status.
Spring Boot
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomMessageHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        return Health.up().withDetail("message", "All systems go!").build();
    }
}
Sample Program

This Spring Boot app has a custom health indicator that adds a "service" detail to the health endpoint. It returns "up" status with a message if the service is running.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

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

@Component
class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        boolean serviceRunning = checkService();
        if (serviceRunning) {
            return Health.up().withDetail("service", "Running smoothly").build();
        } else {
            return Health.down().withDetail("service", "Service down").build();
        }
    }

    private boolean checkService() {
        // Simulate service check logic
        return true;
    }
}
OutputSuccess
Important Notes

Custom health indicators automatically add their info to the main health endpoint at /actuator/health.

You can combine multiple health indicators for a full health report.

Remember to enable actuator endpoints in your application.properties if needed.

Summary

Health endpoint shows app status and can be customized to add checks.

Create a class implementing HealthIndicator and override health().

Use Health.up() or Health.down() with details to customize output.