Health checks with Spring Boot Actuator
45 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A load balancer must decide whether to send customers to this copy of ShopLite. If it can only check that the port is open, an app with a dead database still looks healthy.
Adding Actuator and calling /actuator/health
Spring Boot Actuator is a Boot module that adds ready-made endpoints, such as /actuator/health, which tell other programs how the running app is doing.
spring-boot-starter-actuator is one dependency; with it, every Boot web app gets /actuator/health. The answer is JSON with a status, and the HTTP code matches it: 200 for UP, 503 for DOWN.
Example 1 · pom.xml: the Actuator starter, and the default health answer
Runs in: the config-lab practice project with Spring Web MVC, Actuator, JDBC and the ShopLite database
<!-- pom.xml, inside <dependencies> --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>$ curl -i localhost:8080/actuator/health
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{"groups":["liveness","readiness"],"status":"UP"}
$ curl -i localhost:8080/actuator
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true}}}
$ curl -i localhost:8080/actuator/env
HTTP/1.1 404
Content-Type: application/json
{"timestamp":"2026-10-16T06:57:55.207Z","status":404,"error":"Not Found","path":"/actuator/env"}What this code does
- With no Actuator settings,
/actuator/healthshows only the overall status and the names of the two probe groups./actuatorlists what is exposed: just health. /actuator/envanswers 404: the endpoint exists but is not exposed over HTTP. The 404 body is Boot's default error JSON, becauseconfig-labhas no ProblemDetail handler.- In Windows PowerShell 5.1,
curlis a short name forInvoke-WebRequest; typecurl.exe -i ...there to run the real curl.
Why does an API need this? On a server, something must decide whether to send traffic to your app and whether to restart it. Without a health endpoint, that tool can only check that the port is open, and an app with a dead database connection still looks fine.
The lessons from here on run config-lab as a web app: add spring-boot-starter-webmvc and spring-boot-starter-actuator to its pom.xml, next to the JDBC dependencies from the secrets lesson, and start it with the DB_* variables set.
Exposing only the endpoints you need
Over HTTP, Boot exposes only health by default; the other endpoints answer 404. management.endpoints.web.exposure.include=health,info names exactly what is reachable.
Example 2 · Exposing health and info, nothing else
Runs in: the config-lab practice project with Spring Web MVC, Actuator, JDBC and the ShopLite database
# src/main/resources/application.properties# Actuator: only health and info over HTTPmanagement.endpoints.web.exposure.include=health,info$ curl -i localhost:8080/actuator
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true}}}
$ curl -i localhost:8080/actuator/env
HTTP/1.1 404
Content-Type: application/json
{"timestamp":"2026-10-16T06:58:18.094Z","status":404,"error":"Not Found","path":"/actuator/env"}What this code does
- The links list now has
infoandhealth.envis still 404, so nobody can read your settings over HTTP. - The setting takes a comma-separated list of endpoint ids.
excludealso exists and wins overinclude.
Actuator has many more endpoints: env (every setting), configprops, beans, loggers, heapdump (a copy of the JVM memory), threaddump, shutdown and others. Keep the list short in prod: env and configprops show settings, and a heap dump can contain passwords, tokens and customer data that happened to be in memory. Never use include=* on a public server.
When you need the powerful endpoints, expose them only on a separate port that is not reachable from the internet (management.server.port), or protect them with Spring Security, which comes in a later section.
Fix the error
Fix the exposure setting
Each setting causes a problem. Pick the line that replaces the marked one.
Round 1 of 3
The deployment tool needs /actuator/info, but it answers 404.
Error
GET /actuator/info -> 404 Not Found
Health details and the database health indicator
A health indicator is a bean that checks one thing, such as the database, and reports a status. The overall /actuator/health status is the worst of all indicators.
Boot adds indicators automatically for what it finds: db when a DataSource exists, diskSpace, ping and more. management.endpoint.health.show-details decides how much of them you see.
Example 3 · show-details=always: the db component and its friends
Runs in: the config-lab practice project with Spring Web MVC, Actuator, JDBC and the ShopLite database
# src/main/resources/application.properties (dev values)spring.datasource.hikari.connection-timeout=3000management.endpoint.health.show-details=always$ curl -i localhost:8080/actuator/health
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{
"components": {
"db": {
"details": {
"database": "MySQL",
"validationQuery": "isValid()"
},
"status": "UP"
},
"diskSpace": {
"details": {
"total": 237483585536,
"free": 29909639168,
"threshold": 10485760,
"path": "...",
"exists": true
},
"status": "UP"
},
"invoiceFolder": {
"details": {
"folder": "invoices"
},
"status": "UP"
},
"livenessState": {
"status": "UP"
},
"ping": {
"status": "UP"
},
"readinessState": {
"status": "UP"
},
"ssl": {
"details": {
"expiringChains": [],
"invalidChains": [],
"validChains": []
},
"status": "UP"
}
},
"groups": [
"liveness",
"readiness"
],
"status": "UP"
}
$ curl -i localhost:8080/actuator/health/db
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{
"details": {
"database": "MySQL",
"validationQuery": "isValid()"
},
"status": "UP"
}
# Later, with DB_URL pointing to a port where no MySQL runs:
$ curl -i localhost:8080/actuator/health/db
HTTP/1.1 503
Content-Type: application/vnd.spring-boot.actuator.v3+json
{"details":{"error":"org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection"},"status":"DOWN"}What this code does
- The JSON is shown formatted (curl prints it on one line) and the disk path is shortened.
dbsays MySQL answered;invoiceFolderis the custom indicator from Example 6. - When MySQL cannot be reached,
dbis DOWN with the error and the HTTP status is 503. The shortconnection-timeout(3 s) makes the check fail fast instead of waiting 30 s. - Without
show-details=alwaysthe same app shows only{"groups":[...],"status":"UP"}, and/actuator/health/dbis a 404.
The db indicator borrows a connection and checks it is valid; others cover disk space, ssl and the liveness and readiness states. show-details takes never (the default: only the overall status), when-authorized (details only for authenticated users) or always.
Use always in dev; in prod use never or when-authorized, because details reveal your database type and internal errors. Each component also has its own URL, such as /actuator/health/db. With details hidden, those sub-paths answer 404.
/actuator/info: version and Git commit
/actuator/info shows facts about the running build: artifact, version and build time from the Maven plugin's build-info goal, and the Git branch and short commit id from git-commit-id-maven-plugin.
Example 4 · pom.xml plugins and info.* settings for /actuator/info
Runs in: the config-lab practice project with Spring Web MVC, Actuator, JDBC and the ShopLite database
# pom.xml<!-- pom.xml, inside <build><plugins> --><plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> <!-- writes META-INF/build-info.properties --> </goals> </execution> </executions></plugin><plugin> <!-- writes git.properties; the version comes from the Spring Boot parent --> <groupId>io.github.git-commit-id</groupId> <artifactId>git-commit-id-maven-plugin</artifactId></plugin># src/main/resources/application.propertiesmanagement.info.env.enabled=trueinfo.app.description=ShopLite configuration labinfo.app.java-version=${java.specification.version}$ curl -i localhost:8080/actuator/info
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{
"app": {
"description": "ShopLite configuration lab",
"java-version": "19"
},
"git": {
"branch": "master",
"commit": {
"id": "1175684",
"time": "2026-10-16T05:19:10Z"
}
},
"build": {
"artifact": "config-lab",
"name": "config-lab",
"time": "2026-10-16T05:21:08.366Z",
"version": "0.0.1-SNAPSHOT",
"group": "com.shoplite"
}
}What this code does
buildcomes frombuild-info.propertiesandgitfromgit.properties; both files are written into the jar by./mvnw package. The Git plugin needs the project to be a Git repository with at least one commit.appcomes from theinfo.app.*settings (the second file).java-versionshows the Java that runs the jar: 17 or 21 on your computer.- The commit id
1175684is the short hash of the commit the jar was built from; compare it withgit log --oneline.
The info endpoint must be in the exposure list. It answers a classic production question: "which version is running on the server right now?" If a bug appears after a deploy, the commit id tells you exactly which code to look at.
management.info.env.enabled=true also shows every info.* setting, such as info.app.description. Like any setting, they can use placeholders, for example ${java.specification.version}.
Liveness, readiness and a custom HealthIndicator
Liveness asks whether the app is broken beyond repair, so a platform should restart it. Readiness asks whether the app may receive traffic right now.
Deployment platforms such as Kubernetes ask two questions: should this app be restarted, and may it receive traffic? Boot 4.1 serves /actuator/health/liveness and /actuator/health/readiness out of the box.
Example 5 · Liveness and readiness while the database is down
Runs in: the config-lab practice project with Spring Web MVC, Actuator, JDBC and the ShopLite database
# Started with the database unreachable (DB_URL points to a port with no MySQL)# and this setting, so readiness also checks the database:management.endpoint.health.group.readiness.include=readinessState,db$ curl -i localhost:8080/actuator/health/db
HTTP/1.1 503
Content-Type: application/vnd.spring-boot.actuator.v3+json
{
"details": {
"error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection"
},
"status": "DOWN"
}
$ curl -i localhost:8080/actuator/health/liveness
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{
"status": "UP"
}
$ curl -i localhost:8080/actuator/health/readiness
HTTP/1.1 503
Content-Type: application/vnd.spring-boot.actuator.v3+json
{
"components": {
"db": {
"details": {
"error": "org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection"
},
"status": "DOWN"
},
"readinessState": {
"status": "UP"
}
},
"status": "DOWN"
}What this code does
- Liveness stays 200: the app itself is fine, so a platform would not restart it.
- Readiness is 503 because the group now includes
db: a load balancer stops sending requests to this copy until MySQL is back, then readiness turns UP again on its own.
Example 6 · InvoiceFolderHealthIndicator.java: a custom check
Runs in: the config-lab practice project with Spring Web MVC, Actuator, JDBC and the ShopLite database
// src/main/java/com/shoplite/lab/health/InvoiceFolderHealthIndicator.javapackage com.shoplite.lab.health;
import java.nio.file.Files;import java.nio.file.Path;
import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.health.contributor.Health;import org.springframework.boot.health.contributor.HealthIndicator;import org.springframework.stereotype.Component;
@Componentpublic class InvoiceFolderHealthIndicator implements HealthIndicator {
private final Path folder;
public InvoiceFolderHealthIndicator(@Value("${shoplite.invoice-dir:invoices}") String folder) { this.folder = Path.of(folder); }
@Override public Health health() { if (Files.isDirectory(folder) && Files.isWritable(folder)) { return Health.up().withDetail("folder", folder.toString()).build(); } return Health.down() .withDetail("folder", folder.toString()) .withDetail("reason", "missing or not writable, so invoices cannot be saved") .build(); }}# With the folder present (part of /actuator/health):
"invoiceFolder": { "details": { "folder": "invoices" }, "status": "UP" }
# After deleting the invoices folder:
$ curl -i localhost:8080/actuator/health/invoiceFolder
HTTP/1.1 503
Content-Type: application/vnd.spring-boot.actuator.v3+json
{"details":{"folder":"invoices","reason":"missing or not writable, so invoices cannot be saved"},"status":"DOWN"}
$ curl -i localhost:8080/actuator/health/readiness
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
{"status":"UP"}What this code does
- Spring finds the bean like any
@Component; its nameinvoiceFolderHealthIndicatorgives the component nameinvoiceFolderand the URL/actuator/health/invoiceFolder. - When the folder is missing, the component is DOWN with a reason, and the overall
/actuator/healthbecomes DOWN with HTTP 503 as well. - Readiness stays UP because the readiness group does not include this component. Add it to the group only if the app truly cannot serve requests without it.
By default both groups check only the app's own state, not the database. If an instance without a database should get no traffic, add the check to readiness only: management.endpoint.health.group.readiness.include=readinessState,db. Never add outside systems to liveness, or a short database outage restarts every copy of your app at once.
For anything your app depends on, write a custom HealthIndicator: a bean with one health() method that returns Health.up(), Health.down() or Health.outOfService() with details. In Boot 4 the interface is org.springframework.boot.health.contributor.HealthIndicator; the bean name minus HealthIndicator becomes the component name.
Predict the output
Which HTTP code?
Read the setup, then pick the answer a platform would see.
Round 1 of 3
MySQL is stopped. What do the two probes return?
Next step
Check your understanding
11 quick questions