Bird
0
0

Which approach correctly combines ResponseEntityExceptionHandler methods and @ExceptionHandler to achieve this?

hard📝 Application Q15 of 15
Spring Boot - Exception Handling
You want to customize error responses for both validation errors and JSON parse errors in your Spring Boot REST API. Which approach correctly combines ResponseEntityExceptionHandler methods and @ExceptionHandler to achieve this?
AOverride <code>handleMethodArgumentNotValid</code> for validation errors and add an @ExceptionHandler for <code>HttpMessageNotReadableException</code>
BUse only @ExceptionHandler methods for all exceptions, no overrides needed
COverride <code>handleHttpMessageNotReadable</code> for validation errors and use @ExceptionHandler for <code>MethodArgumentNotValidException</code>
DOverride <code>handleExceptionInternal</code> to catch all exceptions globally
Step-by-Step Solution
Solution:
  1. Step 1: Understand which methods handle which exceptions

    handleMethodArgumentNotValid handles validation errors; handleHttpMessageNotReadable handles JSON parse errors.
  2. Step 2: Combine overrides and @ExceptionHandler correctly

    Override handleMethodArgumentNotValid for validation errors and use @ExceptionHandler for HttpMessageNotReadableException to customize JSON parse error responses.
  3. Final Answer:

    Override handleMethodArgumentNotValid for validation errors and add an @ExceptionHandler for HttpMessageNotReadableException -> Option A
  4. Quick Check:

    Use overrides for validation, @ExceptionHandler for JSON errors [OK]
Quick Trick: Override validation method, @ExceptionHandler for JSON parse errors [OK]
Common Mistakes:
  • Mixing exception types in wrong handlers
  • Using only @ExceptionHandler without overrides
  • Overriding wrong methods for given exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes