๐Ÿ”ฅ Launch Offer: Interview Preparation Bundle @ โ‚น2,499 โ‚น9,999 โ€ข 75% OFF. Offer expires in ๐Ÿ”ฅ Launch Offer: Interview Preparation Bundle @ โ‚น2,499 โ‚น9,999 โ€ข 75% OFF. Offer expires in Interview Preparation Bundle @ โ‚น2,499 โ‚น9,999

00:00:00 Get Bundle
home spring boot global-exception-handling-in-spring-boot-using-responseentityexceptionhandler
Interview guide Spring Boot 16 sections

Global Exception Handling in Spring Boot Using ResponseEntityExceptionHandler

Extending ResponseEntityExceptionHandler in Spring Boot This is one of the most important concepts in Spring Boot exception handling and is asked frequently in interviews. If you already know:โ€ฆ

Start reading
By javainterviewprep June 15, 2026 5 min read

Extending ResponseEntityExceptionHandler in Spring Boot

This is one of the most important concepts in Spring Boot exception handling and is asked frequently in interviews.

If you already know:

  • โœ… @ExceptionHandler
  • โœ… @ControllerAdvice
  • โœ… @RestControllerAdvice

then this is the next topic you should learn.


What is ResponseEntityExceptionHandler?

ResponseEntityExceptionHandler is a pre-built class provided by Spring.

Instead of writing exception handlers for all Spring exceptions yourself, Spring already provides methods for handling many common exceptions.

You can simply extend this class and override the methods you want to customize.

public class ResponseEntityExceptionHandler {
...
}

Spring has already written handlers for exceptions like:

  • Validation exceptions
  • Missing request parameters
  • Unsupported HTTP methods
  • Type mismatch
  • Missing request body
  • Media type not supported
  • etc.

Without ResponseEntityExceptionHandler

Suppose a user sends

GET /users

But your controller expects

@GetMapping
public List<User> getUsers(
@RequestParam String city) {

}

Request

GET /users

No city parameter.

Spring throws

MissingServletRequestParameterException

Without extending ResponseEntityExceptionHandler, you write

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(
MissingServletRequestParameterException.class)
public ResponseEntity<String> handle(
MissingServletRequestParameterException ex){

return ResponseEntity.badRequest()
.body(ex.getMessage());
}
}

Works perfectly.

But imagine handling 25 different Spring exceptions…

That becomes repetitive.


With ResponseEntityExceptionHandler

Simply extend it.

@RestControllerAdvice
public class GlobalExceptionHandler
extends ResponseEntityExceptionHandler {

}

Now Spring already knows how to handle many exceptions.

You only customize the response.


Example 1

Missing Request Parameter

Controller

@RestController
public class UserController {

@GetMapping("/users")
public String getUsers(
@RequestParam String city){

return city;
}

}

Request

GET /users

Output

400 Bad Request

Spring throws

MissingServletRequestParameterException

Override the handler

@RestControllerAdvice
public class GlobalExceptionHandler
extends ResponseEntityExceptionHandler {

@Override
protected ResponseEntity<Object>
handleMissingServletRequestParameter(

MissingServletRequestParameterException ex,

HttpHeaders headers,

HttpStatusCode status,

WebRequest request) {

Map<String,Object> error =
new HashMap<>();

error.put("message",
ex.getParameterName()
+ " parameter is required");

error.put("status",400);

return new ResponseEntity<>(
error,
HttpStatus.BAD_REQUEST);

}

}

Response

{
"message":"city parameter is required",
"status":400
}

Notice:

We never wrote

@ExceptionHandler

Spring automatically called this overridden method.


Example 2

HTTP Method Not Supported

Controller

@PostMapping("/users")
public String saveUser(){

return "Saved";
}

User calls

GET /users

Spring throws

HttpRequestMethodNotSupportedException

Override

@Override
protected ResponseEntity<Object>
handleHttpRequestMethodNotSupported(

HttpRequestMethodNotSupportedException ex,

HttpHeaders headers,

HttpStatusCode status,

WebRequest request){

Map<String,Object> error =
new HashMap<>();

error.put("message",
"This API supports only "
+ ex.getSupportedHttpMethods());

error.put("status",405);

return new ResponseEntity<>(
error,
HttpStatus.METHOD_NOT_ALLOWED);

}

Output

{
"message":"This API supports only [POST]",
"status":405
}

Example 3

Validation Error

Suppose

public class UserRequest {

@NotBlank
private String name;

}

Controller

@PostMapping("/users")
public String saveUser(
@Valid
@RequestBody UserRequest request){

return "Saved";
}

Input

{
}

Spring throws

MethodArgumentNotValidException

Override

@Override
protected ResponseEntity<Object>
handleMethodArgumentNotValid(

MethodArgumentNotValidException ex,

HttpHeaders headers,

HttpStatusCode status,

WebRequest request){

Map<String,Object> errors =
new HashMap<>();

ex.getBindingResult()
.getFieldErrors()
.forEach(error ->

errors.put(
error.getField(),
error.getDefaultMessage()));

return new ResponseEntity<>(
errors,
HttpStatus.BAD_REQUEST);

}

Output

{
"name":"must not be blank"
}

This is the most commonly overridden method in Spring Boot projects.


Can We Still Use @ExceptionHandler?

Yes.

A common pattern is to combine both approaches:

@RestControllerAdvice
public class GlobalExceptionHandler
extends ResponseEntityExceptionHandler {

}

Override Spring exceptions:

@Override
protected ResponseEntity<Object>
handleMethodArgumentNotValid(...)

Handle custom exceptions with @ExceptionHandler:

@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<String> handleUserNotFound(
UserNotFoundException ex){

return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(ex.getMessage());

}

This combination is considered a best practice.


Which Exceptions Does It Already Handle?

ResponseEntityExceptionHandler includes handlers for many common framework exceptions, including:

ExceptionTypical HTTP StatusWhen it happens
MethodArgumentNotValidException400@Valid validation fails
MissingServletRequestParameterException400Required query parameter is missing
HttpRequestMethodNotSupportedException405Wrong HTTP method is used
HttpMediaTypeNotSupportedException415Unsupported Content-Type
HttpMediaTypeNotAcceptableException406Requested response media type cannot be produced
MissingPathVariableException500 (or configurable)Required path variable is missing
NoHandlerFoundException404No endpoint matches the request (when configured)
AsyncRequestTimeoutException503Async request times out
HandlerMethodValidationException (Spring 6.1+)400Method parameter validation fails

@ExceptionHandler vs ResponseEntityExceptionHandler

Feature@ExceptionHandlerResponseEntityExceptionHandler
Handles custom exceptionsโœ… YesโŒ No (use @ExceptionHandler instead)
Handles Spring framework exceptionsโœ… Yesโœ… Yes
Built into SpringโŒ Noโœ… Yes
Requires overriding methodsโŒ Noโœ… Yes
Best for business logic exceptionsโœ… YesโŒ No
Best for validation and request errorsโš ๏ธ Possible, but more manualโœ… Yes

Real Project Structure

A common enterprise setup looks like this:

com.example.exception
โ”‚
โ”œโ”€โ”€ GlobalExceptionHandler.java
โ”œโ”€โ”€ ApiError.java
โ”œโ”€โ”€ ErrorResponse.java
โ”œโ”€โ”€ UserNotFoundException.java
โ”œโ”€โ”€ InvalidAgeException.java
โ””โ”€โ”€ DuplicateEmailException.java

GlobalExceptionHandler.java

@RestControllerAdvice
public class GlobalExceptionHandler
extends ResponseEntityExceptionHandler {

// Override Spring exception handlers
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(...) {
// Handle validation errors
}

@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(...) {
// Handle missing request parameters
}

// Handle custom business exceptions
@ExceptionHandler(UserNotFoundException.class)
public ResponseEntity<ApiError> handleUserNotFound(UserNotFoundException ex) {
// Build and return custom error response
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ApiError> handleGeneral(Exception ex) {
// Fallback for unexpected exceptions
}
}

Interview Answer

What is ResponseEntityExceptionHandler?

ResponseEntityExceptionHandler is a Spring-provided base class that contains default handlers for many common Spring MVC exceptions, such as validation failures, missing request parameters, unsupported HTTP methods, and media type errors. By extending this class in a @RestControllerAdvice, we can override only the handlers we want to customize while still benefiting from Spring’s built-in exception handling. We typically use it together with @ExceptionHandler for handling our own custom business exceptions.


Best Practice

In production Spring Boot applications:

  • โœ… Extend ResponseEntityExceptionHandler to customize responses for Spring MVC exceptions.
  • โœ… Use @ExceptionHandler for custom business exceptions (for example, UserNotFoundException or DuplicateEmailException).
  • โœ… Return a consistent error response object (for example, including timestamp, status, error, message, and path) for all exceptions.
  • โœ… Keep all global exception handling logic centralized in a single @RestControllerAdvice class or a small set of well-organized advice classes.

This approach results in clean, maintainable, and consistent API error handling that is widely used in enterprise Spring Boot and microservices applications.