Spring Boot

Exception Handling

Exception handling with @ControllerAdvice, @ExceptionHandler, @ResponseStatus, error standardization

25 Interview-Fragen·
Mid-Level
1

What is @ControllerAdvice in Spring MVC?

Antwort

@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.

2

How to handle a specific exception in a controller?

Antwort

@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.

3

What is the purpose of @ResponseStatus on an exception class?

Antwort

@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.

4

Which exception is thrown when validation with @Valid fails?

5

What is ProblemDetail in Spring 6+?

+22 Interview-Fragen

Meistere Spring Boot für dein nächstes Interview

Zugang zu allen Fragen, Flashcards, technischen Tests, Code-Review-Übungen und Interview-Simulatoren.

Kostenlos starten