0
0
Spring Bootframework~30 mins

Why monitoring matters in Spring Boot - See It in Action

Choose your learning style9 modes available
Why monitoring matters
📖 Scenario: You are running a small Spring Boot web application that serves users. To keep your app healthy and responsive, you want to monitor its status and key metrics like uptime and request count.
🎯 Goal: Build a simple Spring Boot app with a health check endpoint and a counter for requests. Then add monitoring logic to track and display these metrics.
📋 What You'll Learn
Create a Spring Boot application class
Add a REST controller with a health check endpoint
Add a request counter variable
Increment the counter on each request
Print the current health status and request count
💡 Why This Matters
🌍 Real World
Monitoring helps keep applications running smoothly by alerting you to problems early. This simple app shows how to track basic health and usage.
💼 Career
DevOps engineers and backend developers often build and maintain monitoring endpoints to ensure system reliability and performance.
Progress0 / 4 steps
1
Create Spring Boot application class
Create a Spring Boot application class called MonitoringApp with the @SpringBootApplication annotation and a main method that runs the app using SpringApplication.run(MonitoringApp.class, args).
Spring Boot
Need a hint?

Use @SpringBootApplication on the class and write a main method that calls SpringApplication.run.

2
Add REST controller with health check
Add a REST controller class called HealthController with the @RestController annotation. Inside it, create a method healthCheck mapped to /health using @GetMapping that returns the string "UP".
Spring Boot
Need a hint?

Use @RestController on the class and @GetMapping("/health") on the method that returns "UP".

3
Add request counter and increment logic
Inside the HealthController class, add a private integer variable called requestCount initialized to 0. Modify the healthCheck method to increment requestCount by 1 each time it is called.
Spring Boot
Need a hint?

Declare private int requestCount = 0; and add requestCount++; inside the healthCheck method.

4
Print health status and request count
Modify the healthCheck method to return a string showing both the health status and the current requestCount in this format: "Status: UP, Requests: X" where X is the value of requestCount. Use String.format to build the string.
Spring Boot
Need a hint?

Use return String.format("Status: UP, Requests: %d", requestCount); to show the status and count.