Sub topic 14
Next steps: DevTools and Actuator
This chapter assumes you can generate a Spring Boot project, run it with ./mvnw spring-boot:run, and add a simple REST endpoint. It also assumes you can edit application.properties. DevTools and Actuator are optional. Neither is needed to run an application, but both help once you start changing and operating one.
What each tool does
- Spring Boot DevTools is a development-time module. It restarts the application when classes on the classpath change and applies sensible development defaults.
- Spring Boot Actuator adds production-oriented endpoints. They report the application's health, build info, metrics, and configuration.
Both are added as starters, which you met earlier. Auto-configuration then wires them up with no extra code.
Adding DevTools
Add the dependency to the pom.xml you already have. The optional flag keeps it from being passed on to projects that depend on yours.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>Now run ./mvnw spring-boot:run. Change the text returned by HelloController, then recompile. Your IDE does this on save when automatic build is on. Maven can do it with ./mvnw compile in a second terminal. DevTools notices the changed class file and restarts the application context. This restart is usually faster than a cold start. DevTools uses two class loaders. Your own classes are reloaded on each restart, and unchanged library classes stay loaded.
DevTools is turned off automatically when the app runs from a fully packaged JAR with java -jar. The repackaged executable JAR also excludes it by default, so it does not reach production.
Adding Actuator
Add the starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Restart the app and call the health endpoint:
curl http://localhost:8080/actuator/healthThe response is a small JSON document:
{"status":"UP"}By default, over HTTP, Actuator exposes only health. This is deliberate, because other endpoints can reveal internal details. You choose what to expose in application.properties:
management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=alwaysAfter restarting, /actuator/metrics lists metric names, and /actuator/health includes details such as disk space.
How the pieces connect
Common errors
- No restart happens. DevTools reacts to classpath changes, not to edits of
.javafiles alone. Make sure the code is recompiled. /actuator/infoor/actuator/metricsreturns 404. These endpoints are not exposed over HTTP by default. Add them tomanagement.endpoints.web.exposure.include.- Exposing everything in production. Using
include=*makes every enabled endpoint reachable. Expose only what you need and protect the rest, for example with Spring Security.
Where to go from here
You now have a project that is generated, configured, runnable, packaged, and observable. Good next topics are Spring MVC in depth, Spring Data for persistence, Spring Security, and writing tests with spring-boot-starter-test.
Exercise
You add the Actuator starter and call /actuator/metrics, but you get a 404. Health works. What is the likely cause, and what property do you change?
Show answer
Only the health endpoint is exposed over HTTP by default. Add metrics to the exposure list: management.endpoints.web.exposure.include=health,metrics. Then restart the app.
Exercise
Why is it safe to leave the DevTools dependency in pom.xml when you build the executable JAR?
Show answer
The repackaged executable JAR excludes DevTools by default. It is also disabled when the app runs with java -jar. Marking it optional also stops it from passing to dependent projects.