Spring Controller Method Arguments And Return Values
@RequestMapping handler methods have a flexible signature and can choose from a range of supported controller method arguments and return values.
Method Arguments
The next table describes the supported controller method arguments.
Controller method argument | Description |
---|---|
|
Generic access to request parameters and request and session attributes, without direct use of the Servlet API. |
|
Choose any specific request or response type — for example, |
|
Enforces the presence of a session. As a consequence, such an argument is never |
|
Servlet 4.0 push builder API for programmatic HTTP/2 resource pushes.
Note that, per the Servlet specification, the injected |
|
Currently authenticated user — possibly a specific |
|
The HTTP method of the request. |
|
The current request locale, determined by the most specific |
|
The time zone associated with the current request, as determined by a |
|
For access to the raw request body as exposed by the Servlet API. |
|
For access to the raw response body as exposed by the Servlet API. |
|
For access to URI template variables. See URI patterns. |
|
For access to name-value pairs in URI path segments. See Matrix Variables. |
|
For access to the Servlet request parameters, including multipart files. Parameter values
are converted to the declared method argument type. See Note that use of |
|
For access to request headers. Header values are converted to the declared method argument
type. See |
|
For access to cookies. Cookies values are converted to the declared method argument
type. See |
|
For access to the HTTP request body. Body content is converted to the declared method
argument type by using |
|
For access to request headers and body. The body is converted with an |
|
For access to a part in a |
|
For access to the model that is used in HTML controllers and exposed to templates as part of view rendering. |
|
Specify attributes to use in case of a redirect (that is, to be appended to the query string) and flash attributes to be stored temporarily until the request after redirect. See Redirect Attributes and Flash Attributes. |
|
For access to an existing attribute in the model (instantiated if not present) with
data binding and validation applied. See Note that use of |
|
For access to errors from validation and data binding for a command object
(that is, a |
|
For marking form processing complete, which triggers cleanup of session attributes
declared through a class-level |
|
For preparing a URL relative to the current request’s host, port, scheme, context path, and the literal part of the servlet mapping. See URI Links. |
|
For access to any session attribute, in contrast to model attributes stored in the session
as a result of a class-level |
|
For access to request attributes. See |
Any other argument |
If a method argument is not matched to any of the earlier values in this table and it is
a simple type (as determined by
BeanUtils#isSimpleProperty,
it is a resolved as a |
Return Values
The next table describes the supported controller method return values.
Controller method return value | Description |
---|---|
|
The return value is converted through |
|
The return value that specifies the full response (including HTTP headers and body) is to be converted
through |
|
For returning a response with headers and no body. |
|
A view name to be resolved with |
|
A |
|
Attributes to be added to the implicit model, with the view name implicitly determined
through a |
|
An attribute to be added to the model, with the view name implicitly determined through
a Note that |
|
The view and model attributes to use and, optionally, a response status. |
|
A method with a If none of the above is true, a |
|
Produce any of the preceding return values asynchronously from any thread — for example, as a
result of some event or callback. See Asynchronous Requests and |
|
Produce any of the above return values asynchronously in a Spring MVC-managed thread.
See Asynchronous Requests and |
|
Alternative to |
|
Emit a stream of objects asynchronously to be written to the response with
|
|
Write to the response |
Reactive types — Reactor, RxJava, or others through |
Alternative to For streaming scenarios (for example, See Asynchronous Requests and Reactive Types. |
Any other return value |
Any return value that does not match any of the earlier values in this table and that
is a |