This project demonstrates how to implement global exception handling in a Spring Boot application using the @ControllerAdvice annotation. It showcases custom exception handling by defining a centralized mechanism for managing exceptions across the application.
- Centralized exception handling using
@ControllerAdvice - Custom exception classes (
CustomerNotFoundException) - ResponseEntity customization for HTTP responses
- RESTful API endpoints for managing customers
The project follows a simple structure that separates the controller, entity, and exception-related files for better organization and maintainability:
-
CustomerController
- This class provides endpoints for retrieving all customers and individual customers by their ID.
- If a requested customer is not found, it throws a
CustomerNotFoundException.
-
GlobalExceptionHandler
- The class uses the
@ControllerAdviceannotation to define a global exception handler. - It captures any
CustomerNotFoundExceptionthrown by the controller and returns a structured exception response (in the form of anExceptionObject).
- The class uses the
-
ExceptionObject
- This class represents the structure of the exception response. It contains:
message: The error message describing what went wrong.timestamp: The time when the error occurred.httpStatus: The HTTP status code associated with the error (e.g.,NOT_FOUND).
- This class represents the structure of the exception response. It contains:
-
CustomerNotFoundException
- A custom exception class used to represent when a customer is not found in the system.
Endpoint: /customers/all
Method: GET
This endpoint retrieves a list of all customers in the system.
Endpoint: /customers/get/{customerId}
Method: GET
This endpoint retrieves a customer by their ID. If the customer with the given ID is not found, it will throw a CustomerNotFoundException and return a custom error response.
-
CustomerController provides the REST API for fetching customer data.
GET /customers/all: Returns a list of all customers.GET /customers/get/{customerId}: Fetches a specific customer by their ID.
-
GlobalExceptionHandler captures exceptions like
CustomerNotFoundExceptionand returns an error response containing:message: The error message explaining the issue.timestamp: The time the error occurred.httpStatus: The HTTP status code (e.g.,404 NOT_FOUND).
-
ExceptionObject is the structure for the exception response, making it easy to return consistent and informative error details in case of an exception.
-
Fetch All Customers
To fetch all customers, make aGETrequest to/customers/all. The response will return a list of customers in JSON format. -
Fetch Customer by ID
To fetch a customer by ID, make aGETrequest to/customers/get/{customerId}. If the customer is found, their details will be returned. If not, an error response will be returned indicating that the customer is not found.
- Spring Boot: Framework for building the Java-based application.
- Spring Web: Module used to build RESTful web services.
- Spring Data JPA: Used for database interaction with the
Customerentity. - Lombok: Used to reduce boilerplate code like getters and setters.
- JPA: Used for ORM (Object-Relational Mapping) with the database.
This project demonstrates the use of Spring Boot’s @ControllerAdvice to handle exceptions globally. By centralizing exception handling in one place, it ensures consistent error responses and simplifies debugging. This setup allows for a more maintainable codebase by separating concerns and ensuring that error handling is both reusable and extensible.