Exception Handling
Exception handling with @ControllerAdvice, @ExceptionHandler, @ResponseStatus, error standardization
1What is @ControllerAdvice in Spring MVC?
What is @ControllerAdvice in Spring MVC?
답변
@ControllerAdvice allows centralizing exception handling for all application controllers. This annotation creates a global component that intercepts exceptions thrown by any @Controller or @RestController. This avoids duplicating error handling logic in each controller and promotes error response consistency.
2How to handle a specific exception in a controller?
How to handle a specific exception in a controller?
답변
@ExceptionHandler allows defining a method that handles one or more specific exceptions in a controller or @ControllerAdvice. The annotated method is automatically invoked when the specified exception is thrown. It's possible to return a ResponseEntity to control the HTTP status and response body.
3What is the purpose of @ResponseStatus on an exception class?
What is the purpose of @ResponseStatus on an exception class?
답변
@ResponseStatus on a custom exception allows automatically mapping this exception to a specific HTTP status code. When the exception is thrown, Spring automatically returns the indicated HTTP status without requiring an @ExceptionHandler. This approach simplifies code for simple exceptions that directly correspond to an HTTP status.
Which exception is thrown when validation with @Valid fails?
What is ProblemDetail in Spring 6+?
+22 면접 질문