Spring MVC - @ResponseStatus on Exception Classes MCQ
0/13
0✓
0✗
1. What does @ResponseStatus do when it is placed on a custom exception class?
Why: An unhandled exception whose class has @ResponseStatus carries that HTTP status code to the client.
2. What status code does the client normally get when an unhandled exception is thrown and the exception class has no @ResponseStatus?
Why: Without @ResponseStatus, an unhandled exception normally results in an HTTP 500 response.
3. Which annotation should be placed on the exception class so that it returns HTTP 403?
// Add annotation here
public class AccessDeniedException
extends Exception {
}
Why:@ResponseStatus takes an HttpStatus value and applies it to the response when this exception is thrown.
4. Which class in Spring MVC is responsible for applying the status code from @ResponseStatus on an exception?
Why:ResponseStatusExceptionResolver is a HandlerExceptionResolver implementation that reads @ResponseStatus from exception classes.
5. Do you need extra configuration to use @ResponseStatus on exception classes?
Why: By default, the resolver that handles @ResponseStatus is auto-registered, so no extra setup is needed.
6. A controller method throws the exception below. What status code does the client get?
@ResponseStatus(HttpStatus.NOT_FOUND)
class ItemMissingException
extends Exception {
}
@RequestMapping("/item")
@ResponseBody
public String get() throws Exception {
throw new ItemMissingException();
}
Why: The exception class is annotated with NOT_FOUND, so the client receives HTTP 404.
7. A handler method throws a plain Exception that wraps a custom exception annotated with @ResponseStatus(HttpStatus.FORBIDDEN). What status code is sent (Spring 4.2 or later)?
throw new Exception(
new UserNotLoggedInException(null));
Why: Since Spring 4.2, the resolver checks cause exceptions recursively, so the status of the wrapped exception is used.
8. What happens when a @ResponseStatus exception is also handled by an @ExceptionHandler method?
Why: If the exception is handled elsewhere, such as by @ExceptionHandler, the status on the exception class is not applied.
9. Which approach lets you set a status code from inside a handler method without creating a new exception class (Spring 5.0 and later)?
Why:ResponseStatusException lets you set the status, and an optional reason, directly from the handler method.
10. Which code correctly sets the status with a reason using ResponseStatusException?
@RequestMapping("/admin")
@ResponseBody
public String admin() {
// throw here
}
Why:ResponseStatusException is an exception, so it is thrown. Its constructor takes an HttpStatus and an optional reason.
11. Where else can @ResponseStatus be used, besides exception classes?
Why:@ResponseStatus can be used on handler methods, on the controller class (inherited by its methods), and on @ExceptionHandler methods.
12. When is a custom exception class with @ResponseStatus a good choice over ResponseStatusException?
Why: A dedicated exception class is still preferred when the exception type itself carries meaning in other parts of the application. ResponseStatusException suits cases where the status is set programmatically.