Method Arguments

The next table describes the supported controller method arguments. Reactive types are not supported for any arguments.spring-doc.cn

JDK 8’s java.util.Optional is supported as a method argument in combination with annotations that have a required attribute (for example, @RequestParam, @RequestHeader, and others) and is equivalent to required=false.spring-doc.cn

Controller method argument Description

WebRequest, NativeWebRequestspring-doc.cn

Generic access to request parameters and request and session attributes, without direct use of the Servlet API.spring-doc.cn

jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponsespring-doc.cn

Choose any specific request or response type — for example, ServletRequest, HttpServletRequest, or Spring’s MultipartRequest, MultipartHttpServletRequest.spring-doc.cn

jakarta.servlet.http.HttpSessionspring-doc.cn

Enforces the presence of a session. As a consequence, such an argument is never null. Note that session access is not thread-safe. Consider setting the RequestMappingHandlerAdapter instance’s synchronizeOnSession flag to true if multiple requests are allowed to concurrently access a session.spring-doc.cn

jakarta.servlet.http.PushBuilderspring-doc.cn

Servlet 4.0 push builder API for programmatic HTTP/2 resource pushes. Note that, per the Servlet specification, the injected PushBuilder instance can be null if the client does not support that HTTP/2 feature.spring-doc.cn

java.security.Principalspring-doc.cn

Currently authenticated user — possibly a specific Principal implementation class if known.spring-doc.cn

Note that this argument is not resolved eagerly, if it is annotated in order to allow a custom resolver to resolve it before falling back on default resolution via HttpServletRequest#getUserPrincipal. For example, the Spring Security Authentication implements Principal and would be injected as such via HttpServletRequest#getUserPrincipal, unless it is also annotated with @AuthenticationPrincipal in which case it is resolved by a custom Spring Security resolver through Authentication#getPrincipal.spring-doc.cn

HttpMethodspring-doc.cn

The HTTP method of the request.spring-doc.cn

java.util.Localespring-doc.cn

The current request locale, determined by the most specific LocaleResolver available (in effect, the configured LocaleResolver or LocaleContextResolver).spring-doc.cn

java.util.TimeZone + java.time.ZoneIdspring-doc.cn

The time zone associated with the current request, as determined by a LocaleContextResolver.spring-doc.cn

java.io.InputStream, java.io.Readerspring-doc.cn

For access to the raw request body as exposed by the Servlet API.spring-doc.cn

java.io.OutputStream, java.io.Writerspring-doc.cn

For access to the raw response body as exposed by the Servlet API.spring-doc.cn

@PathVariablespring-doc.cn

For access to URI template variables. See URI patterns.spring-doc.cn

@MatrixVariablespring-doc.cn

For access to name-value pairs in URI path segments. See Matrix Variables.spring-doc.cn

@RequestParamspring-doc.cn

For access to the Servlet request parameters, including multipart files. Parameter values are converted to the declared method argument type. See @RequestParam as well as Multipart.spring-doc.cn

Note that use of @RequestParam is optional for simple parameter values. See “Any other argument”, at the end of this table.spring-doc.cn

@RequestHeaderspring-doc.cn

For access to request headers. Header values are converted to the declared method argument type. See @RequestHeader.spring-doc.cn

@CookieValuespring-doc.cn

For access to cookies. Cookies values are converted to the declared method argument type. See @CookieValue.spring-doc.cn

@RequestBodyspring-doc.cn

For access to the HTTP request body. Body content is converted to the declared method argument type by using HttpMessageConverter implementations. See @RequestBody.spring-doc.cn

HttpEntity<B>spring-doc.cn

For access to request headers and body. The body is converted with an HttpMessageConverter. See HttpEntity.spring-doc.cn

@RequestPartspring-doc.cn

For access to a part in a multipart/form-data request, converting the part’s body with an HttpMessageConverter. See Multipart.spring-doc.cn

java.util.Map, org.springframework.ui.Model, org.springframework.ui.ModelMapspring-doc.cn

For access to the model that is used in HTML controllers and exposed to templates as part of view rendering.spring-doc.cn

RedirectAttributesspring-doc.cn

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.spring-doc.cn

@ModelAttributespring-doc.cn

For access to an existing attribute in the model (instantiated if not present) with data binding and validation applied. See @ModelAttribute as well as Model and DataBinder.spring-doc.cn

Note that use of @ModelAttribute is optional (for example, to set its attributes). See “Any other argument” at the end of this table.spring-doc.cn

Errors, BindingResultspring-doc.cn

For access to errors from validation and data binding for a command object (that is, a @ModelAttribute argument) or errors from the validation of a @RequestBody or @RequestPart arguments. You must declare an Errors, or BindingResult argument immediately after the validated method argument.spring-doc.cn

SessionStatus + class-level @SessionAttributesspring-doc.cn

For marking form processing complete, which triggers cleanup of session attributes declared through a class-level @SessionAttributes annotation. See @SessionAttributes for more details.spring-doc.cn

UriComponentsBuilderspring-doc.cn

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.spring-doc.cn

@SessionAttributespring-doc.cn

For access to any session attribute, in contrast to model attributes stored in the session as a result of a class-level @SessionAttributes declaration. See @SessionAttribute for more details.spring-doc.cn

@RequestAttributespring-doc.cn

For access to request attributes. See @RequestAttribute for more details.spring-doc.cn

Any other argumentspring-doc.cn

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 resolved as a @RequestParam. Otherwise, it is resolved as a @ModelAttribute.spring-doc.cn