This project demonstrates how to implement custom exception handling in a Spring Boot web application using Aspect-Oriented Programming (AOP). Instead of using Spring Boot's built-in @ControllerAdvice
, this approach allows for dynamic handling of exceptions using custom annotations and an Aspect.
Reinventing the wheel makes you better driver !!
Two custom exception classes are defined for demonstration purposes:
- CustomBadRequestException: Represents a custom bad request exception.
- CustomNotFoundException : Represents a custom not found exception.
public class CustomBadRequestException extends RuntimeException {
// ...
}
public class CustomNotFoundException extends RuntimeException {
// ...
}
Two custom annotations are used to mark classes and methods for handling exceptions:
@ControllerHandler
: Marks a controller class for exception handling.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ControllerHandler {
}
A sample controller is used to throw custom exceptions. The controller methods are also annotated with @ExceptionHandler
for the same exceptions, as it would be done in a typical Spring Boot application.
@RestController
@RequestMapping("/api/test")
@ControllerHandler // new custom handler
public class SimpleController {
// endpoints
@GetMapping("/not-found")
public ResponseEntity<?> getNotFoundTest() {
throw new CustomNotFoundException("Not Found Resources");
}
@GetMapping("/bad-request")
public ResponseEntity<?> getBadRequestTest() {
throw new CustomBadRequestException("Bad Request");
}
// methods handler for custom exceptions
@ExceptionHandler(CustomBadRequestException.class)
public ResponseEntity<?> handleBadRequestException(CustomBadRequestException e) {
// Handle bad request exception
}
@ExceptionHandler(CustomNotFoundException.class)
public ResponseEntity<?> handleNotFoundException(CustomNotFoundException e) {
// Handle not found exception
}
}
The ControllersExceptionHandlingAspect.java class is an Aspect that intercepts exceptions thrown within classes marked with @ControllerHandler
and handles them dynamically based on the @ExceptionHandler
annotations.
@Aspect
@Component
public class ControllersExceptionHandlingAspect {
@AfterThrowing(
pointcut = "@within(com.example.customcontrolleradvicewithaop.annotations.ControllerHandler)",
throwing = "exception"
)
public ResponseEntity<?> handleCustomExceptions(Exception exception) {
// Handles custom exceptions dynamically
}
// ...
}
- Spring Boot : Used for building the web application.
- Spring AOP : Used for Aspect-Oriented Programming.
- Java 8 or higher : used
17
.
- Clone this repository.
- Ensure you have Maven and Java installed.
- Build and run the project using Maven:
mvn spring-boot:run
- Access the following endpoints in your browser or API tool (e.g., Postman) to see exception handling in action:
http://localhost:8080/api/test/not-found http://localhost:8080/api/test/bad-request