🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
The @ControllerAdvice annotation in Spring Boot is a specialization that allows you to handle exceptions across multiple controllers. This annotation helps build a centralized exception-handling mechanism, reducing exception-handling redundancy in individual controllers.
Key Points
1. @ControllerAdvice is ideal for global exception handling, data binding configurations, and model enhancements.
2. It enhances all controllers or a specific subset of them.
3. Often used with @ExceptionHandler, @ModelAttribute, and @InitBinder annotations within the class it annotates.
2. Development Steps
1. Create a @ControllerAdvice annotated class.
2. Define methods using @ExceptionHandler to handle specific exceptions.
3. Optionally use @ModelAttribute and @InitBinder for additional controller enhancements.
3. Implementation Example
// Step 1: Define custom exceptions
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
public class DataFormatException extends RuntimeException {
public DataFormatException(String message) {
super(message);
}
}
// Step 2: Create a @ControllerAdvice class
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
@ControllerAdvice
public class GlobalControllerAdvice {
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred: " + ex.getMessage());
}
@ExceptionHandler(NullPointerException.class)
public ResponseEntity<String> handleNullPointerException(NullPointerException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Null pointer exception: " + ex.getMessage());
}
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<String> handleUserNotFoundException(UserNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found: " + ex.getMessage());
}
@ExceptionHandler(DataFormatException.class)
public ResponseEntity<String> handleDataFormatException(DataFormatException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Data format error: " + ex.getMessage());
}
}
// Step 3: Example controller to demonstrate the use of ControllerAdvice
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SimpleController {
@GetMapping("/error")
public String throwError() {
throw new NullPointerException("Intentional Null Pointer");
}
@GetMapping("/user")
public String findUser() {
throw new UserNotFoundException("User ID not found");
}
@GetMapping("/data")
public String dataFormat() {
throw new DataFormatException("Incorrect data format");
}
}
Explanation:
1. GlobalControllerAdvice is annotated with @ControllerAdvice, making it applicable to handle exceptions thrown by controllers across the entire application.
2. Custom exceptions such as UserNotFoundException and DataFormatException are defined to handle specific business logic-related errors.
3. Within GlobalControllerAdvice, methods are defined with @ExceptionHandler for different types of exceptions. These methods ensure appropriate HTTP responses based on the type of exception encountered.
4. SimpleController includes methods that simulate different scenarios, showcasing the global exception-handling capabilities provided by the @ControllerAdvice.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment