Complete the code to define a global exception handler class in Spring Boot.
@[1] public class GlobalExceptionHandler { }
The @ControllerAdvice annotation marks a class as a global exception handler in Spring Boot.
Complete the method annotation to handle exceptions of type RuntimeException.
@ExceptionHandler([1].class) public ResponseEntity<String> handleRuntimeException(RuntimeException ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage()); }
The @ExceptionHandler(RuntimeException.class) annotation tells Spring Boot to use this method for RuntimeException errors.
Fix the error in the method signature to correctly return a ResponseEntity with a message.
public [1] handleNotFoundException(EntityNotFoundException ex) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Entity not found"); }
The method must return ResponseEntity<String> to send HTTP status and message properly.
Fill both blanks to create a method that handles IllegalArgumentException and returns a BAD_REQUEST status.
@ExceptionHandler([1].class) public [2] handleIllegalArgument(IllegalArgumentException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage()); }
The method handles IllegalArgumentException and returns a ResponseEntity<String> with BAD_REQUEST status.
Fill all three blanks to create a global handler method for NullPointerException with INTERNAL_SERVER_ERROR status and a custom message.
@ExceptionHandler([1].class) public [2] handleNullPointer(NullPointerException ex) { return ResponseEntity.status(HttpStatus.[3]).body("Null value encountered"); }
This method handles NullPointerException, returns a ResponseEntity<String> with INTERNAL_SERVER_ERROR status and a custom message.