ResponseEntityExceptionHandler — Base Class for Spring MVC Exception Handling
Fast recall
Quick revision
Core answer
Knowledge of Spring's built-in exception handling infrastructure and when to leverage framework utilities versus writing custom code
Writing @ExceptionHandler methods for standard Spring exceptions that ResponseEntityExceptionHandler already handles, creating unnecessary duplicate code
Explain how to override handleMethodArgumentNotValid() to customize validation error responses while keeping the built-in 400 status code behavior
Interview-ready guide
Interview answer
The base class knows how to map exceptions such as MethodArgumentNotValidException and HttpRequestMethodNotSupportedException to suitable ResponseEntity results. A subclass can replace the body with one stable API error DTO while retaining framework headers and status. ResourceNotFound from the service layer still needs an application handler. Boot's fallback /error path and static-resource handling can also participate in 404 responses, so 'extending this class automatically handles every 404' is incorrect.
ResponseEntityExceptionHandler — Base Class for Spring MVC Exception Handling
Direct Answer
ResponseEntityExceptionHandler is a base class for a @ControllerAdvice that centralizes handling of Spring MVC exceptions and returns ResponseEntity results. It supplies handlers and status mappings for many framework exceptions, but it does not automatically create your application's desired error-body format. Some conditions, including an unmapped URL, depend on MVC and Boot configuration and are not universally delivered as NoHandlerFoundException.
Code Example
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
// Override to customize validation error response
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request) {
var errors = ex.getBindingResult().getFieldErrors().stream()
.map(err -> err.getField() + ": " + err.getDefaultMessage())
.toList();
var errorResponse = new ValidationErrorResponse(
"Validation failed",
errors,
LocalDateTime.now()
);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
}
}
record ValidationErrorResponse(String message, List<String> errors, LocalDateTime timestamp) {}
Common Mistakes & Pitfalls
- Handling Spring exceptions twice: Freshers often write an
@ExceptionHandler(MethodArgumentNotValidException.class)method in their advice class that already extends ResponseEntityExceptionHandler — this creates a conflict because the base class already handles it. The correct approach is to override the protectedhandleMethodArgumentNotValid()method instead. - Not calling super when needed: When overriding a handle method, freshers sometimes write completely custom logic and forget that calling
super.handleXxx()can provide a good starting point. If you just want to add logging, call super and wrap the result. - Wrong exception names: Confusing
MethodArgumentNotValidException(validation failures on @Valid DTOs) withConstraintViolationException(validation failures on method parameters with @Validated on the class). ResponseEntityExceptionHandler only handles the first one by default.
Check your understanding
Answer these without looking back at the article.
- What standard Spring MVC exceptions does ResponseEntityExceptionHandler handle?
- How do you customize the validation error response using ResponseEntityExceptionHandler?
- What happens to exceptions not handled by ResponseEntityExceptionHandler?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.