Annotation Type 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
ResponseEntityor"redirect:".Warning: when using this annotation on an exception class, or when setting the
reasonattribute of this annotation, theHttpServletResponse.sendErrormethod 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 areasonunsuitable for REST APIs. For such cases it is preferable to use aResponseEntityas a return type and avoid the use of@ResponseStatusaltogether.Note that a controller class may also be annotated with
@ResponseStatusand is then inherited by all@RequestMappingmethods.- Since:
- 3.0
- Author:
- Arjen Poutsma, Sam Brannen
- See Also:
ResponseStatusExceptionResolver,HttpServletResponse.sendError(int, String)
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description HttpStatuscodeThe status code to use for the response.StringreasonThe reason to be used for the response.HttpStatusvalueAlias forcode().
Element Detail
value
@AliasFor("code") HttpStatus value
Alias forcode().- Default:
- 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.- Since:
- 4.2
- See Also:
HttpServletResponse.setStatus(int),HttpServletResponse.sendError(int)
- Default:
- org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR