Annotation Interface ResponseStatus

@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented public @interface ResponseStatus
Marks a method or exception class with the status code() and reason() that should be returned.

The status code is applied to the HTTP response when the handler method is invoked, but does not override status information set by other means, such as ResponseEntity or "redirect:".

Warning: when using this annotation on an exception class, or when setting the reason attribute of this annotation, the HttpServletResponse.sendError method will be used.

With HttpServletResponse.sendError, the response is considered complete and should not be written to any further. Furthermore, the Servlet container will typically write an HTML error page therefore making the use of a reason unsuitable for REST APIs. For such cases it is preferable to use a ResponseEntity as a return type and avoid the use of @ResponseStatus altogether.

Note that a controller class may also be annotated with @ResponseStatus which is then inherited by all @RequestMapping and @ExceptionHandler methods in that class and its subclasses unless overridden by a local @ResponseStatus declaration on the method.

Arjen Poutsma, Sam Brannen
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    The status code to use for the response.
    The reason to be used for the response.
    Alias for code().