Top Description Methods
org.springframework.web.bind.annotation

public @Interface ResponseStatus

extends Annotation
Annotations
@Target:TYPE, METHOD
@Retention:RUNTIME
@Documented
Imports
java.lang.annotation.Documented, .ElementType, .Retention, .RetentionPolicy, .Target, org.springframework.core.annotation.AliasFor, org.springframework.http.HttpStatus

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 org.springframework.http.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.

Authors
Arjen Poutsma, Sam Brannen
Since
3.0
See Also
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver, jakarta.servlet.http.HttpServletResponse#sendError(int, String)

Method Summary

Modifier and TypeMethod and Description
public HttpStatus
code()

The status code to use for the response.

public String
reason()

The reason to be used for the response.

public HttpStatus
value()

Alias for code.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

codeback to summary
public HttpStatus code()

The status code to use for the response.

Default is HttpStatus#INTERNAL_SERVER_ERROR, which should typically be changed to something more appropriate.

Annotations
@AliasFor:value
Since
4.2
See Also
jakarta.servlet.http.HttpServletResponse#setStatus(int), jakarta.servlet.http.HttpServletResponse#sendError(int)
reasonback to summary
public String reason()

The reason to be used for the response.

Defaults to an empty string which will be ignored. Set the reason to a non-empty value to have it used to send a Servlet container error page. In this case, the return value of the handler method will be ignored.

See Also
jakarta.servlet.http.HttpServletResponse#sendError(int, String)
valueback to summary
public HttpStatus value()

Alias for code.

Annotations
@AliasFor:code