Spring MVC and Web Layer
August 23, 2021
M05 Q10 What other annotations might you use on a controller method parameter?
@RequestParam
- access to the Servlet request parameters, including multipart files, parameters will be automatically converted to declared method argument types, parameters can be made optional with usage ofrequired
attribute orOptional
from Java 8, for optional request parametersdefaultValue
can be set as well
@PathVariable
โ access to URI template variables, parameters can be made optional with usage ofrequired
attribute orOptional
from Java 8
@MatrixVariable
- access to name-value pairs in URI path segments as described in RFC 3986, allows mapping variables from requests like/employees/id=1;name=John
@CookieValue
- bind the value of an HTTP cookie to a method argument in a controller, you can bind against simple types orCookie
class, cookie can be set with usage ofHttpServletResponse
, cookie can be set as required or optional viarequired
attribute or withOptional
from Java 8, when usingrequired
attribute,defaultValue
can be used as well
@RequestHeader
โ access request header values or all header key and values when binding against aMap
@RequestBody
โ allows access to HTTP request body, content will be converted to method controller type byHttpMessageConverter
, request body can be made optional with usage ofrequired
attribute or Java 8Optional
, can be used with@Valid
for bean validation
@RequestPart
โ allows to bind multipart HTTP requests to method parameter, content will be converted to method controller type, request part can be made optional with usage ofrequired
attribute or Java 8Optional
, can be used with@Valid
for bean validation
@RequestAttribute
โ allows access to HTTP request attributes populated on serverside during HTTP request by filter or interceptor, can be made optional with usage ofrequired
attribute or Java 8Optional
@ModelAttribute
- access to an existing attribute in the model (instantiated if not present) with data binding and validation applied
@SessionAttribute
- access to pre-existing session attributes that are managed globally, can be made optional with usage ofrequired
attribute or Java 8Optional
@SessionAttributes
- used to store model attributes in the HTTP Servlet session between requests, useful for multi-step from processing