Bird
0
0

In a class extending ResponseEntityExceptionHandler, which method would you override to customize the response when a client sends an unsupported media type?

medium📝 component behavior Q4 of 15
Spring Boot - Exception Handling
In a class extending ResponseEntityExceptionHandler, which method would you override to customize the response when a client sends an unsupported media type?
Aprotected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request)
Bprotected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request)
Cprotected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request)
Dprotected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request)
Step-by-Step Solution
Solution:
  1. Step 1: Identify the exception type for unsupported media type

    The exception thrown when a client sends an unsupported media type is HttpMediaTypeNotSupportedException.
  2. Step 2: Find the corresponding handler method in ResponseEntityExceptionHandler

    The method to override is handleHttpMediaTypeNotSupported with the signature:
    protected ResponseEntity handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request).
  3. Final Answer:

    protected ResponseEntity handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) correctly overrides the method to handle unsupported media type exceptions.
  4. Quick Check:

    Exception type matches method name and parameters [OK]
  5. Quick Trick: Unsupported media type uses handleHttpMediaTypeNotSupported [OK]
    Common Mistakes:
    • Confusing method for unsupported HTTP method instead of media type
    • Using wrong exception class in method signature
    • Overriding methods unrelated to media type errors

    Want More Practice?

    15+ quiz questions · All difficulty levels · Free

    Free Signup - Practice All Questions
    More Spring Boot Quizzes