@Target(value={TYPE,METHOD}) @Retention(value=RUNTIME) @Documented public @interface ResponseStatus
code()
and
reason()
that should be returned.
The status code is applied to the HTTP response when the handler method is invoked.
Note: when using this annotation on an exception class,
or when setting the reason
attribute of the 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 servlet container will typically write an HTML error page
therefore making the use of a reason unsuitable for REST APIs.
For such cases prefer the use of ResponseEntity
as a return type and avoid ResponseStatus
altogether.
ResponseStatusExceptionResolver
,
HttpServletResponse.sendError(int, String)
Modifier and Type | Optional Element and Description |
---|---|
HttpStatus |
code
The status code to use for the response.
|
String |
reason
The reason to be used for the response.
|
HttpStatus |
value
Alias for
code() . |
@AliasFor(value="code") public abstract HttpStatus value
code()
.@AliasFor(value="value") public abstract HttpStatus code
Default is HttpStatus.INTERNAL_SERVER_ERROR
, which should
typically be changed to something more appropriate.
HttpServletResponse.setStatus(int)
,
HttpServletResponse.sendError(int)
public abstract String reason