注释类型 ResponseStatus
@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented public @interface ResponseStatus
Marks a method or exception class with the statuscode()
andreason()
that should be returned.The status code is applied to the HTTP response when the handler method is invoked and overrides status information set by other means, like
ResponseEntity
or"redirect:"
.Warning: when using this annotation on an exception class, or when setting the
reason
attribute of this annotation, theHttpServletResponse.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 areason
unsuitable for REST APIs. For such cases it is preferable to use aResponseEntity
as a return type and avoid the use of@ResponseStatus
altogether.Note that a controller class may also be annotated with
@ResponseStatus
and is then inherited by all@RequestMapping
methods.- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Sam Brannen
- 另请参阅:
ResponseStatusExceptionResolver
,HttpServletResponse.sendError(int, String)
可选元素概要
可选元素 修饰符和类型 可选元素 说明 HttpStatus
code
The status code to use for the response.String
reason
The reason to be used for the response.HttpStatus
value
Alias forcode()
.
元素详细资料
value
@AliasFor("code") HttpStatus value
Alias forcode()
.- 默认值:
- org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
code
@AliasFor("value") 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.- 从以下版本开始:
- 4.2
- 另请参阅:
HttpServletResponse.setStatus(int)
,HttpServletResponse.sendError(int)
- 默认值:
- org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR