public String findOwner(**@PathVariable** String ownerId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
@ -807,20 +807,21 @@ template variable by name. You can specify it in the annotation:
@@ -807,20 +807,21 @@ template variable by name. You can specify it in the annotation:
public String findOwner(**@PathVariable** String ownerId, Model model) {
// implementation omitted
}
@ -832,7 +833,7 @@ A method can have any number of `@PathVariable` annotations:
@@ -832,7 +833,7 @@ A method can have any number of `@PathVariable` annotations:
public String findPet(**@PathVariable** String ownerId, **@PathVariable** String petId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
Pet pet = owner.getPet(petId);
@ -844,7 +845,7 @@ A method can have any number of `@PathVariable` annotations:
@@ -844,7 +845,7 @@ A method can have any number of `@PathVariable` annotations:
When a `@PathVariable` annotation is used on a `Map<String, String>` argument, the map
is populated with all URI template variables.
A URI template can be assembled from type and path level __@RequestMapping__
A URI template can be assembled from type and method level __@RequestMapping__
annotations. As a result the `findPet()` method can be invoked with a URL such as
`/owners/42/pets/21`.
@ -863,7 +864,7 @@ annotations. As a result the `findPet()` method can be invoked with a URL such a
@@ -863,7 +864,7 @@ annotations. As a result the `findPet()` method can be invoked with a URL such a
}
----
A `@PathVariable` argument can be of __any simple type__ such as int, long, Date, etc.
A `@PathVariable` argument can be of __any simple type__ such as `int`, `long`, `Date`, etc.
Spring automatically converts to the appropriate type or throws a
`TypeMismatchException` if it fails to do so. You can also register support for parsing
additional data types. See <<mvc-ann-typeconversion>> and <<mvc-ann-webdatabinder>>.
@ -876,24 +877,24 @@ Sometimes you need more precision in defining URI template variables. Consider t
@@ -876,24 +877,24 @@ Sometimes you need more precision in defining URI template variables. Consider t
The `@RequestMapping` annotation supports the use of regular expressions in URI template
variables. The syntax is `{varName:regex}` where the first part defines the variable
name and the second - the regular expression.For example:
name and the second - the regular expression.For example:
public void handle(@PathVariable String version, @PathVariable String extension) {
// ...
}
public void handle(@PathVariable String version, @PathVariable String extension) {
// ...
}
----
[[mvc-ann-requestmapping-patterns]]
==== Path Patterns
In addition to URI templates, the `@RequestMapping` annotation also supports Ant-style
path patterns (for example, `/myPath/{asterisk}.do`). A combination of URI template variables and
Ant-style globs is also supported (e.g. `/owners/{asterisk}/pets/{petId}`).
In addition to URI templates, the `@RequestMapping` annotation and all _composed_
`@RequestMapping` variants also support Ant-style path patterns (for example,
`/myPath/{asterisk}.do`). A combination of URI template variables and Ant-style globs is
also supported (e.g. `/owners/{asterisk}/pets/{petId}`).
[[mvc-ann-requestmapping-pattern-comparison]]
@ -923,7 +924,7 @@ can be customized (see <<mvc-config-path-matching>> in the section on configurin
@@ -923,7 +924,7 @@ can be customized (see <<mvc-config-path-matching>> in the section on configurin
[[mvc-ann-requestmapping-placeholders]]
==== Path Patterns with Placeholders
Patterns in `@RequestMapping` annotations support ${...} placeholders against local
Patterns in `@RequestMapping` annotations support `${...}` placeholders against local
properties and/or system properties and environment variables. This may be useful in
cases where the path a controller is mapped to may need to be customized through
configuration. For more information on placeholders, see the javadocs of the
@ -1030,7 +1031,7 @@ Below is an example of extracting the matrix variable "q":
@@ -1030,7 +1031,7 @@ Below is an example of extracting the matrix variable "q":
public void findPet(@PathVariable String petId, @MatrixVariable int q) {
// petId == 42
@ -1047,7 +1048,7 @@ specific to identify where the variable is expected to be:
@@ -1047,7 +1048,7 @@ specific to identify where the variable is expected to be:
@MatrixVariable(name="q", pathVar="ownerId") int q1,
@MatrixVariable(name="q", pathVar="petId") int q2) {
@ -1065,7 +1066,7 @@ A matrix variable may be defined as optional and a default value specified:
@@ -1065,7 +1066,7 @@ A matrix variable may be defined as optional and a default value specified:
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
@ -1253,15 +1252,17 @@ respectively instead. They are intended specifically for that purpose.
@@ -1253,15 +1252,17 @@ respectively instead. They are intended specifically for that purpose.
`@RequestMapping` methods mapped to "GET" are also implicitly mapped to "HEAD",
i.e. there is no need to have "HEAD" explicitly declared. An HTTP HEAD request
is processed as if it was an HTTP GET except instead of writing the body only
is processed as if it were an HTTP GET except instead of writing the body only
the number of bytes are counted and the "Content-Length" header set.
`@RequestMapping` method have built-in support for HTTP OPTIONS. By default an
`@RequestMapping` methods have built-in support for HTTP OPTIONS. By default an
HTTP OPTIONS request is handled by setting the "Allow" response header to the
HTTP methods explicitly declared on all `@RequestMapping` methods with matching
URL patterns. When no HTTP methods are explicitly declared the "Allow" header
is set to "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS". Ideally always declare the
HTTP method(s) an `@RequestMapping` method is intended to handle.
HTTP method(s) that an `@RequestMapping` method is intended to handle, or alternatively
use one of the dedicated _composed_ `@RequestMapping` variants (see
<<mvc-ann-requestmapping-composed>>).
Although not necessary an `@RequestMapping` method can be mapped to and handle
either HTTP HEAD or HTTP OPTIONS, or both.
@ -1272,9 +1273,9 @@ either HTTP HEAD or HTTP OPTIONS, or both.
@@ -1272,9 +1273,9 @@ either HTTP HEAD or HTTP OPTIONS, or both.
[[mvc-ann-methods]]
=== Defining @RequestMapping handler methods
An `@RequestMapping` handler method can have a very flexible signatures. The supported
`@RequestMapping` handler methods can have very flexible signatures. The supported
method arguments and return values are described in the following section. Most
arguments can be used in arbitrary order with the only exception of `BindingResult`
arguments can be used in arbitrary order with the only exception being `BindingResult`
public String processSubmit(**@ModelAttribute("pet") Pet pet**, Model model, **BindingResult result**) { ... }
----
@ -1388,7 +1389,7 @@ this working you have to reorder the parameters as follows:
@@ -1388,7 +1389,7 @@ this working you have to reorder the parameters as follows:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@RequestMapping(method = RequestMethod.POST)
@PostMapping
public String processSubmit(**@ModelAttribute("pet") Pet pet**, **BindingResult result**, Model model) { ... }
----
@ -1468,7 +1469,7 @@ The following code snippet shows the usage:
@@ -1468,7 +1469,7 @@ The following code snippet shows the usage:
// ...
@RequestMapping(method = RequestMethod.GET)
@GetMapping
public String setupForm(**@RequestParam("petId") int petId**, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
@ -1500,7 +1501,7 @@ be bound to the value of the HTTP request body. For example:
@@ -1500,7 +1501,7 @@ be bound to the value of the HTTP request body. For example:
public String processSubmit(**@ModelAttribute Pet pet**) { }
----
@ -1749,9 +1750,9 @@ using an URI template variable and a type converter. Here is an example:
@@ -1749,9 +1750,9 @@ using an URI template variable and a type converter. Here is an example:
@ -2309,7 +2310,7 @@ is an example of such a controller method:
@@ -2309,7 +2310,7 @@ is an example of such a controller method:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@RequestMapping(method=RequestMethod.POST)
@PostMapping
public Callable<String> processUpload(final MultipartFile file) {
return new Callable<String>() {
@ -3006,7 +3007,7 @@ through `Model` nor `RedirectAttributes`. For example:
@@ -3006,7 +3007,7 @@ through `Model` nor `RedirectAttributes`. For example:
@ -3326,7 +3327,7 @@ Spring MVC also provides a mechanism for building links to controller methods. F
@@ -3326,7 +3327,7 @@ Spring MVC also provides a mechanism for building links to controller methods. F
@RequestMapping("/hotels/{hotel}")
public class BookingController {
@RequestMapping("/bookings/{booking}")
@GetMapping("/bookings/{booking}")
public String getBooking(@PathVariable Long booking) {
// ...
@ -3791,7 +3792,7 @@ use `MultipartHttpServletRequest` or `MultipartFile` in the method parameters:
@@ -3791,7 +3792,7 @@ use `MultipartHttpServletRequest` or `MultipartFile` in the method parameters:
public String onSubmit(**@RequestPart("meta-data") MetaData metadata,
@RequestPart("file-data") MultipartFile file**) {
@ -3937,7 +3938,7 @@ may be more convenient to directly set the status of the response and optionally
@@ -3937,7 +3938,7 @@ may be more convenient to directly set the status of the response and optionally
error content to the body of the response.
You can do that with `@ExceptionHandler` methods. When declared within a controller such
methods apply to exceptions raised by `@RequestMapping` methods of that contoroller (or
methods apply to exceptions raised by `@RequestMapping` methods of that controller (or
any of its sub-classes). You can also declare an `@ExceptionHandler` method within an
`@ControllerAdvice` class in which case it handles exceptions from `@RequestMapping`
methods from many controllers. Below is an example of a controller-local
@ -4487,14 +4488,14 @@ This involves calculating a lastModified `long` and/or an Etag value for a given
@@ -4487,14 +4488,14 @@ This involves calculating a lastModified `long` and/or an Etag value for a given
comparing it against the `'If-Modified-Since'` request header value, and potentially returning
a response with status code 304 (Not Modified).
As described in <<mvc-ann-httpentity>>, Controllers can interact with the request/response using
As described in <<mvc-ann-httpentity>>, controllers can interact with the request/response using
`HttpEntity` types. Controllers returning `ResponseEntity` can include HTTP caching information
in responses like this:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@RequestMapping("/book/{id}")
@GetMapping("/book/{id}")
public ResponseEntity<Book> showBook(@PathVariable Long id) {