Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Your advice extends ResponseEntityExceptionHandler. What does that give you, and what would you customise?

Extending ResponseEntityExceptionHandler gives our advice ready-made handlers for all of Spring MVC's standard exceptions, each returning a ProblemDetail with the right status. We customise it by overriding protected methods such as handleMethodArgumentNotValid to add field errors, by letting detail and title messages come from a MessageSource for translation, and by adding our own @ExceptionHandler methods for domain exceptions in the same class. We must not handle the same Spring exceptions again elsewhere, or the mapping becomes ambiguous or order-dependent.

Why this question is asked

Spring MVC raises about twenty kinds of exceptions of its own: unreadable JSON, a missing parameter, an unsupported media type, a failed @Valid and more. Writing a handler for each one is tedious and easy to get wrong. ResponseEntityExceptionHandler is a base class that already does it, and most production advices extend it. Interviewers ask what it gives us and what we would change, because the defaults are good but not complete for a real API. In this answer, we will recap what the class handles, then customise validation errors, translate messages, add domain handlers, and see the duplicate handler trap.

1What ResponseEntityExceptionHandler Gives Us

ResponseEntityExceptionHandler is an abstract class in Spring MVC meant to be extended by a @ControllerAdvice. It contains one @ExceptionHandler method listing all of Spring MVC's standard exceptions, and it dispatches each to a protected method such as handleHttpRequestMethodNotSupported, handleMissingServletRequestParameter or handleMethodArgumentNotValid. Each of those builds a ResponseEntity whose body is the exception's ProblemDetail, since in Spring Framework 6 all these exceptions implement ErrorResponse and know their own status and body.

All paths end in one method, handleExceptionInternal, which receives the exception, the body, headers, the status and the request. That gives us one place to add common behaviour. Spring Boot's own problem details support is simply an advice that extends this class, and Boot backs off when we define our own subclass.

Step throughHow a standard exception is handled
  1. Spring MVC throwse.g. MethodArgumentNotValidException
  2. handleException dispatchesone @ExceptionHandler for all standard types
  3. handleMethodArgumentNotValidprotected: override to add field errors
  4. handleExceptionInternalcommon hook for every response
  5. createResponseEntityResponseEntity with ProblemDetail body

5 steps in order. Play them, or step through them one at a time.

2Adding Field Errors to Validation Failures

The default response for a failed @Valid body is correct but thin: status 400 and a detail of Invalid request content, with no word about which fields failed. Clients need the list, so we override handleMethodArgumentNotValid.

Java
record FieldIssue(String field, String code, String message) {}
@RestControllerAdvice
class ApiErrorAdvice extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
ProblemDetail pd = ex.getBody(); // already 400, "Invalid request content."
List<FieldIssue> issues = ex.getBindingResult().getFieldErrors().stream()
.map(fe -> new FieldIssue(fe.getField(), fe.getCode(), fe.getDefaultMessage()))
.toList();
pd.setProperty("errors", issues);
return handleExceptionInternal(ex, pd, headers, status, request);
}
}
// 400 {"title": "Bad Request", "status": 400, "detail": "Invalid request content.",
// "errors": [{"field": "quantity", "code": "Positive", "message": "must be greater than 0"}]}

Notice that we start from ex.getBody() rather than a new ProblemDetail, so we keep everything Spring already set, and that we finish through handleExceptionInternal, so our common hook still runs. If our controllers also have parameter constraints, Spring Framework 6.1 and later may report failures as HandlerMethodValidationException, so we override handleHandlerMethodValidationException in the same way.

3Translating Detail Messages With a MessageSource

ResponseEntityExceptionHandler implements MessageSourceAware, so as a Spring bean it receives the application's MessageSource. For each standard exception it then looks up the detail and title using message codes derived from the exception class, and uses the arguments the exception provides. If a code is not found, the built-in English default stays.

Properties
# messages.properties
problemDetail.title.org.springframework.web.bind.MethodArgumentNotValidException=Invalid request
problemDetail.org.springframework.web.bind.MethodArgumentNotValidException=Some fields are not valid.
# messages_de.properties
problemDetail.title.org.springframework.web.bind.MethodArgumentNotValidException=Ungueltige Anfrage
problemDetail.org.springframework.web.bind.MethodArgumentNotValidException=Einige Felder sind ungueltig.
# Accept-Language: de -> "title": "Ungueltige Anfrage", "detail": "Einige Felder sind ungueltig."

The code pattern is problemDetail. plus the fully qualified exception class name for the detail, and problemDetail.title. plus the class name for the title. For field messages we can resolve each FieldError through the same MessageSource with the request locale instead of using its default message. Check your version for the exact codes and arguments of each exception.

4Domain Handlers Alongside

The base class only knows Spring's exceptions. Our domain exceptions get ordinary @ExceptionHandler methods in the same advice, and they should go through handleExceptionInternal too, so every response gets the same shared fields.

Java
@ExceptionHandler(OrderNotFoundException.class)
ResponseEntity<Object> orderNotFound(OrderNotFoundException ex, WebRequest request) {
ProblemDetail pd = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
pd.setProperty("code", "order_not_found");
return handleExceptionInternal(ex, pd, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
}
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
HttpStatusCode status, WebRequest request) {
if (body instanceof ProblemDetail pd) {
pd.setProperty("traceId", currentTraceId()); // added to every response, standard or domain
}
return super.handleExceptionInternal(ex, body, headers, status, request);
}

A good alternative for domain exceptions is to extend ErrorResponseException, which the base class already handles, so they need no handler of their own.

5The Trap: Handling the Same Exceptions Twice

Once we extend the base class, its handler already covers MethodArgumentNotValidException and the others. If we also add our own @ExceptionHandler(MethodArgumentNotValidException.class) in the same class, Spring finds two handler methods for the same type and fails at startup with an IllegalStateException about an ambiguous @ExceptionHandler mapping. The right way is always to override the protected method.

If the duplicate sits in another advice, there is no error, which is worse: whichever advice comes first in order wins, so the response depends on bean ordering and may change after an unrelated change. The rule is one advice that extends ResponseEntityExceptionHandler per application, and no handlers for those exception types anywhere else.

6Follow-up questions

Why does my override of handleMethodArgumentNotValid never run?

Usually because another advice with higher precedence handles the exception first, or because the advice class is not a bean, for example it is outside the scanned packages. Another cause is that the error comes as HandlerMethodValidationException, which has its own method to override.

Do I still need spring.mvc.problemdetails.enabled if I extend the class?

No. The property only registers Spring Boot's own subclass of ResponseEntityExceptionHandler, and Boot skips it when we define ours. Setting it does no harm, but our advice is what handles the exceptions.

Should the catch-all handler for Exception live in this class too?

It can, and inside one advice the closest match wins, so specific handlers still take precedence over the catch-all. The catch-all should return a generic message, log the stack trace and rethrow security exceptions so Spring Security can answer with 401 or 403.

7Conclusion

In this answer, we saw that extending ResponseEntityExceptionHandler gives an advice ready handlers for every standard Spring MVC exception, all ending in handleExceptionInternal with a ProblemDetail body. We overrode handleMethodArgumentNotValid to add field errors, translated details and titles through MessageSource codes, added domain handlers that pass through the same hook, and avoided declaring handlers for the same exceptions again, which is ambiguous in the same class and order-dependent across advices.

What to say in the interviewAbout 80 seconds aloud

Open with

ResponseEntityExceptionHandler gives my advice handlers for all of Spring MVC's standard exceptions out of the box, things like an unsupported method, a missing parameter, unreadable JSON and a failed @Valid, each returning a ResponseEntity with a ProblemDetail body and the right status.

Then, if they want more

They all funnel through handleExceptionInternal, which is the one hook I override to add shared fields like a trace id and to log consistently. What I customise first is handleMethodArgumentNotValid, because the default says only Invalid request content. I take ex.getBody(), add an errors property built from getBindingResult().getFieldErrors() with field, code and message, and return through handleExceptionInternal.

I'd do the same for handleHandlerMethodValidationException, because since 6.1 parameter validation can arrive that way. The class is MessageSourceAware, so I can translate titles and details with codes like problemDetail.title plus the exception's class name. Then I add my own @ExceptionHandler methods for domain exceptions in the same class, or let them extend ErrorResponseException.

The thing to avoid is declaring handlers for the same Spring exceptions again: in the same class Spring fails at startup with an ambiguous mapping, and in another advice it silently depends on order. So one such advice per application.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your advice extends ResponseEntityExceptionHandler. What does that give you, and what...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/57f3bce8b3/