Global REST Error Responses via @ControllerAdvice
@ControllerAdvice
public class GlobalExceptionHandler {
// Global exception handler for BookNotFoundException
@ExceptionHandler(BookNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<String> handleBookNotFoundException(BookNotFoundException ex) {
// Log the exception details for debugging
Logger.log(ex.getMessage());
// Return a user-friendly message and the appropriate HTTP status
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body("Book not found. Please check the provided book ID.");
}
// ... other global exception handlers ...
}PreviousHandling Exceptions in RESTful User ResponsesNextProper vs. Improper Way to Implement CRUD in RESTful Services
Last updated