@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) public ResponseEntity> handleRuntimeException(RuntimeException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Error: " + ex.getMessage()); } }
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(NullPointerException.class) public ResponseEntity> handleNPE(NullPointerException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Null Value: " + ex.getMessage()); } }
@RestController public class ExceptionHandlerController { @ExceptionHandler(RuntimeException.class) public String handleException(RuntimeException ex) { return "Error occurred."; } }
@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(RuntimeException.class) public ResponseEntity> handleRuntimeException(RuntimeException ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + ex.getMessage()); } }