Spring Boot

Exception Handling

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

25 câu hỏi phỏng vấn·
Mid-Level
1

What is @ControllerAdvice in Spring MVC?

Câu trả lời

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

Câu trả lời

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

Câu trả lời

@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 câu hỏi phỏng vấn

Nắm vững Spring Boot cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí