Error handling
25 questions in 7 subtopics.
@ExceptionHandler
Write an@ExceptionHandler that maps OrderNotFoundException to 404 with a JSON body, inside a controller.In what order does Spring MVC try to handle an exception from a controller, and what happens if nothing handles it?How do you build an organisation-wide culture where errors are handled consistently and are useful for operations, not only for API clients?How would you balance exposing detailed error information to help clients against the security risk of leaking internals, as a company-wide policy?@ControllerAdvice
What is@RestControllerAdvice, and how do you limit it to certain controllers?Two @RestControllerAdvice classes exist: one in a shared library handling Exception and one in the service handling OrderNotFoundException. Sometimes the library's generic 500 response is returned for OrderNotFoundException. Why?An exception thrown in a servlet filter returns an HTML or default JSON error instead of your @ControllerAdvice format. Why, and how do you make it consistent?Design a shared error-handling starter for all services. What would it contain and how would teams extend it?ProblemDetail
What isProblemDetail in Spring Framework 6, which standard does it follow, and how do you enable it in Spring Boot?Return a ProblemDetail with status 409, a type URI and a custom property orderId from an @ExceptionHandler.Your advice extends ResponseEntityExceptionHandler. What does that give you, and what would you customise?Your API gateway, Spring Security and services all produce error responses in different formats. Clients see three error shapes for one API. How would you unify them?Error responses design
In production, error responses suddenly contain stack traces. Which properties control this in Spring Boot?Design the error response format for a public API. What fields would you include and what would you never include?Error formats differ across 80 services, which makes client and gateway code complex. How would you create a common error contract and migrate to it?Error contracts outlive the services that produce them. How would you govern error codes across a company for many years so that clients can rely on them?Handling validation errors
Return a 400 response listing each invalid field and its message when@Valid @RequestBody fails.Handle HandlerMethodValidationException from Spring 6.1 method validation and return field errors for parameters.Clients complain that validation error messages change wording in every release and break their parsing. What contract would you define?Validation and business errors are often reported as 500 in older services, which hides real failures in dashboards. How would you change this across the company and measure the effect?Handler exception resolvers
WhichHandlerExceptionResolver implementations does Spring MVC register by default, in what order, and what does each handle?A custom HandlerExceptionResolver that logs and returns JSON for all exceptions was added as a bean, but @ExceptionHandler methods in @ControllerAdvice stopped being called for some exceptions, while for others your resolver never runs. Explain the ordering and fix.Your services are split between Spring MVC and WebFlux, and the error handling mechanisms differ. How would you design a consistent error handling library for both stacks?Whitelabel and custom error pages
What is Spring Boot's Whitelabel Error Page, which component produces it, and how do you replace it with custom HTML pages per status code?A secured Spring Boot app shows a login page or a 401 instead of the custom 404 page for unknown URLs, and the logs show access denied for /error. Why, and how do you fix it?No question matches these choices.