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 ofrequiredattribute orOptionalfrom Java 8, for optional request parametersdefaultValuecan be set as well
@PathVariable– access to URI template variables, parameters can be made optional with usage ofrequiredattribute orOptionalfrom 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 orCookieclass, cookie can be set with usage ofHttpServletResponse, cookie can be set as required or optional viarequiredattribute or withOptionalfrom Java 8, when usingrequiredattribute,defaultValuecan 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 ofrequiredattribute or Java 8Optional, can be used with@Validfor 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 ofrequiredattribute or Java 8Optional, can be used with@Validfor 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 ofrequiredattribute 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 ofrequiredattribute or Java 8Optional
@SessionAttributes- used to store model attributes in the HTTP Servlet session between requests, useful for multi-step from processing