Spring Boot Validation and Error Handling 2 — Questions and Answers
Question 1: Which Spring class can be extended to override the default handling of standard Spring MVC exceptions and return custom error responses?
- AbstractExceptionHandler
- ResponseEntityExceptionHandler (Correct answer)
- GlobalExceptionHandler
- DefaultHandlerExceptionResolver
Correct answer: ResponseEntityExceptionHandler
ResponseEntityExceptionHandler is a convenient base class with @ExceptionHandler methods for standard Spring MVC exceptions that can be overridden to customize the error response.
Question 2: What is the purpose of the @ResponseStatus annotation when placed on a custom exception class?
- It maps the exception to a specific HTTP status code returned when the exception is thrown (Correct answer)
- It marks the exception as a REST-level error visible to clients
- It causes Spring Boot to log the exception at ERROR level automatically
- It registers the exception with the global error handler
Correct answer: It maps the exception to a specific HTTP status code returned when the exception is thrown
@ResponseStatus on an exception class (or @ExceptionHandler method) instructs Spring to respond with the specified HTTP status code whenever that exception propagates to the dispatcher.
Question 3: In Spring Boot 3+, which RFC standard does ProblemDetail implement for structured error responses?
- RFC 7235 (HTTP Authentication)
- RFC 7807 (Problem Details for HTTP APIs) (Correct answer)
- RFC 6749 (OAuth 2.0)
- RFC 9110 (HTTP Semantics)
Correct answer: RFC 7807 (Problem Details for HTTP APIs)
ProblemDetail, introduced in Spring 6 / Spring Boot 3, implements RFC 7807 which defines a standard JSON/XML format for machine-readable HTTP error responses.
Question 4: When validation fails on a @PathVariable or @RequestParam (not @RequestBody), which exception is thrown?
- MethodArgumentNotValidException
- ConstraintViolationException (Correct answer)
- BindException
- MissingServletRequestParameterException
Correct answer: ConstraintViolationException
When Bean Validation constraints are violated on @PathVariable or @RequestParam parameters (requires @Validated on the class), a ConstraintViolationException is thrown rather than MethodArgumentNotValidException.
Question 5: How can you customize the default validation error message for a constraint such as @NotNull?
- Override the message in application.properties using spring.validation.messages.*
- Provide a ValidationMessages.properties file on the classpath with the constraint key (Correct answer)
- Use @MessageSource bean and define messages in messages.properties with the full class name
- Set the message attribute directly on the @NotNull annotation using an EL expression
Correct answer: Provide a ValidationMessages.properties file on the classpath with the constraint key
Bean Validation reads message interpolation keys from ValidationMessages.properties on the classpath; defining javax.validation.constraints.NotNull.message overrides the default.
Question 6: What HTTP status code should a well-designed REST API return when a request body fails Bean Validation?
- 400 Bad Request (Correct answer)
- 422 Unprocessable Entity
- 409 Conflict
- 412 Precondition Failed
Correct answer: 400 Bad Request
400 Bad Request is the conventional status code for malformed or invalid request data including Bean Validation failures in Spring Boot applications.
Question 7: Which Spring Boot property enables the inclusion of binding errors in the /error response body for validation failures?
- server.error.include-binding-errors=always (Correct answer)
- spring.mvc.include-errors=true
- management.error.show-details=always
- server.error.include-message=always
Correct answer: server.error.include-binding-errors=always
Setting server.error.include-binding-errors=always in application.properties includes BindingResult details (field errors and global errors) in the default /error JSON response.
Which Spring class can be extended to override the default handling of standard Spring MVC exceptions and return custom error responses?