Add custom actuator endpoint to Spring Boot Application
[ Spring Boot Actuator
] In this guide, let’s add a custom actuator endpoint to Spring Boot application.
Table of contents
Create spring boot application
Let’s create a very basic spring boot application using Spring Initializr
for the purpose of this tutorial.
Create a Spring Boot starter project using Spring Initializr
Let’s utilize the pre-configured Spring Initializr
which is available here to create actuator-custom-endpoint-basics starter project.
Click on Generate Project. This downloads a zip file containing actuator-custom-endpoint-basics
project. Import the project to your IDE.
Check the default health indicator URL
http://localhost:8080/actuator/health
{
"status": "UP"
}
Create a custom weather endpoint
com.codeaches.actuator.WeatherEndPoint.java
@Component
@Endpoint(id = "weather")
public class WeatherEndPoint {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@ReadOperation
public Map<String, String> check() {
try {
return restTemplate.getForObject("https://api.weather.gov", Map.class);
} catch (Exception e) {
return Collections.singletonMap("status", e.getMessage());
}
}
}
Enable weather endpoint
src\main\resources\application.properties
management.endpoints.web.exposure.include=health,info,weather
Test weather endpoint
http://localhost:8080/actuator/weather
{
"status": "OK"
}
Summary
This concludes our guide to adding a custom actuator end point to Spring Boot Application.
Your feedback is always appreciated. Happy coding!
Related Posts: