Close
« Tutorial

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?

2. What status code does the client normally get when an unhandled exception is thrown and the exception class has no @ResponseStatus?

3. Which annotation should be placed on the exception class so that it returns HTTP 403?

// Add annotation here
public class AccessDeniedException
        extends Exception {
}

4. Which class in Spring MVC is responsible for applying the status code from @ResponseStatus on an exception?

5. Do you need extra configuration to use @ResponseStatus on exception classes?

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();
}

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));

8. What happens when a @ResponseStatus exception is also handled by an @ExceptionHandler method?

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

10. Which code correctly sets the status with a reason using ResponseStatusException?

@RequestMapping("/admin")
@ResponseBody
public String admin() {
    // throw here
}

11. Where else can @ResponseStatus be used, besides exception classes?

12. When is a custom exception class with @ResponseStatus a good choice over ResponseStatusException?

Join