Spring Boot @ControllerAdvice Example

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.

Comments