Supported Method Argument Annotations
All these different method argument types and annotations allow us to write very flexible request handling methods.
RequestParam
The RequestParam annotation can be placed on any argument in a request-handling method. When
present, it is used to retrieve a parameter from the request. When put on a Map, there is some special
handling, depending on whether the name attribute is set. If the name is set, the value is retrieved and
converted into a Map. For conversion, if no name is given, all request parameters are added to the map as key/value pairs.
RequestHeader
The RequestHeader annotation can be placed on any method argument. It is used to bind a method
argument to a request header. When placed on a Map, all available request headers are put in the map as key/value pairs. If it is placed on another type of argument, then the value is converted into the type by using a org.springframework.core.convert.converter.Converter or PropertyEditor.
RequestBody
The RequestBody annotation is used to mark a method parameter we want to bind to the body of the web request. The body is converted into the method parameter type by locating and calling an
org.springframework.http.converter.HttpMessageConverter. This converter is selected based on the
requests content-type. If no converter is found, an
org.springframework.web.HttpMediaTypeNotSupportedException is thrown. By default, this leads to a
response with code 415 (SC_UNSUPPORTED_MEDIA_TYPE) being send to the client.
Optionally, method parameters can also be annotated with javax.validation.Valid or
org.springframework.validation.annotation.Validated to enforce validation for the created object.
RequestPart
When the RequestPart annotation is put on a method argument of the type javax.servlet.http.Part,
org.springframework.web.multipart.MultipartFile (or on a collection or array of the latter,) then we
will get the content of that file (or group of files) injected. If it is put on any other argument type, the
content is passed through an org.springframework.http.converter.HttpMessageConverter for the
content type detected on the file. If no suitable converter is found, then an
org.springframework.web.HttpMediaTypeNotSupportedException is thrown.
ModelAttribute
The ModelAttribute annotation can be placed on method arguments, as well as on methods. When
placed on a method argument, it is used to bind this argument to a model object. When placed on a
method, that method is used to construct a model object, and this method will be called before request handling methods are called. These kinds of methods can be used to create an object to be edited in a form or to supply data needed by a form to render itself.
PathVariable
The PathVariable annotation can be used in conjunction with path variables. Path variables can be used in a URL pattern to bind the URL to a variable. Path variables are denoted as {name}in our URL mapping. If we were to use a URL mapping of /book/{isbn}/image, then isbn would be available as a path variable.
CookieValue
This CookieValue annotation can be placed on any argument in the request-handling method. When
present, it is used to retrieve a cookie. When placed on an argument of type javax.servlet.http.Cookie,
we get the complete cookie. Otherwise, the value of the cookie is converted into the argument type.
Supported Method Return Values
When an arbitrary object is returned and there is no ModelAttribute annotation present, the
framework tries to determine a name to use as the name for the object in the model. It basically takes the simple name of the class (the classname without the package) and lowercases the first letter. For
example, the name of our com.apress.prospringmvc.bookstore.domain.Book becomes book. When the
return type is a collection or array, it becomes the simple name of the class, suffixed with List. Thus a
collection of Book objects becomes bookList.
This same logic is applied when we use a Model or ModelMap to add objects without an explicit name.
This also has the advantage of using the specific objects, instead of a plain Map to gain access to the
underlying implicit model.