Bird
0
0

How can you customize the error response body for a NotFoundException to include a timestamp, message, and path in Spring Boot?

hard📝 Conceptual Q9 of 15
Spring Boot - Exception Handling
How can you customize the error response body for a NotFoundException to include a timestamp, message, and path in Spring Boot?
ACreate an error response class and return it in @ExceptionHandler method with ResponseEntity
BOverride the toString() method in NotFoundException
CSet a custom message in application.properties
DUse @ResponseStatus only without response body
Step-by-Step Solution
Solution:
  1. Step 1: Define a custom error response class

    Create a class with fields like timestamp, message, and path.
  2. Step 2: Return this class instance in @ExceptionHandler with ResponseEntity

    This allows sending structured JSON error details with HTTP status.
  3. Final Answer:

    Create error response class and return it in @ExceptionHandler with ResponseEntity -> Option A
  4. Quick Check:

    Custom error body needs class and ResponseEntity return [OK]
Quick Trick: Use custom error class and ResponseEntity for detailed error JSON [OK]
Common Mistakes:
  • Overriding toString() expecting JSON
  • Using only @ResponseStatus without body
  • Configuring messages in properties only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes