Document composed @RequestMapping variants in the reference manual

Issue: SPR-13995
This commit is contained in:
Sam Brannen 2016-06-07 17:26:57 +02:00
parent 31aed61d15
commit c121f460b1
2 changed files with 64 additions and 7 deletions

View File

@ -611,12 +611,12 @@ application that uses this annotation:
} }
---- ----
In the example, the `@RequestMapping` is used in a number of places. The first usage is In the above example, `@RequestMapping` is used in a number of places. The first usage is
on the type (class) level, which indicates that all handling methods on this controller on the type (class) level, which indicates that all handler methods in this controller
are relative to the `/appointments` path. The `get()` method has a further are relative to the `/appointments` path. The `get()` method has a further
`@RequestMapping` refinement: it only accepts GET requests, meaning that an HTTP GET for `@RequestMapping` refinement: it only accepts `GET` requests, meaning that an HTTP `GET` for
`/appointments` invokes this method. The `add()` has a similar refinement, and the `/appointments` invokes this method. The `add()` has a similar refinement, and the
`getNewForm()` combines the definition of HTTP method and path into one, so that GET `getNewForm()` combines the definition of HTTP method and path into one, so that `GET`
requests for `appointments/new` are handled by that method. requests for `appointments/new` are handled by that method.
The `getForDay()` method shows another usage of `@RequestMapping`: URI templates. (See The `getForDay()` method shows another usage of `@RequestMapping`: URI templates. (See
@ -651,10 +651,66 @@ application shows a multi-action controller using `@RequestMapping`:
} }
---- ----
The above example does not specify GET vs. PUT, POST, and so forth, because The above example does not specify `GET` vs. `PUT`, `POST`, and so forth, because
`@RequestMapping` maps all HTTP methods by default. Use `@RequestMapping(method=GET)` `@RequestMapping` maps all HTTP methods by default. Use `@RequestMapping(method=GET)` or
to narrow the mapping. `@GetMapping` to narrow the mapping.
[[mvc-ann-requestmapping-composed]]
==== Composed @RequestMapping Variants
Spring Framework 4.3 introduces the following method-level _composed_ variants of the
`@RequestMapping` annotation that help to simplify mappings for common HTTP methods and
better express the semantics of the annotated handler method. For example, a
`@GetMapping` can be read as a `GET` `@RequestMapping`.
- `@GetMapping`
- `@PostMapping`
- `@PutMapping`
- `@DeleteMapping`
- `@PatchMapping`
The following example shows a modified version of the `AppointmentsController` from the
previous section that has been simplified with _composed_ `@RequestMapping` annotations.
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@Controller
**@RequestMapping("/appointments")**
public class AppointmentsController {
private final AppointmentBook appointmentBook;
@Autowired
public AppointmentsController(AppointmentBook appointmentBook) {
this.appointmentBook = appointmentBook;
}
**@GetMapping**
public Map<String, Appointment> get() {
return appointmentBook.getAppointmentsForToday();
}
**@GetMapping("/{day}")**
public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
return appointmentBook.getAppointmentsForDay(day);
}
**@GetMapping("/new")**
public AppointmentForm getNewForm() {
return new AppointmentForm();
}
**@PostMapping**
public String add(@Valid AppointmentForm appointment, BindingResult result) {
if (result.hasErrors()) {
return "appointments/new";
}
appointmentBook.addAppointment(appointment);
return "redirect:/appointments";
}
}
----
[[mvc-ann-requestmapping-proxying]] [[mvc-ann-requestmapping-proxying]]
==== @Controller and AOP Proxying ==== @Controller and AOP Proxying

View File

@ -674,6 +674,7 @@ Spring 4.3 also improves the caching abstraction as follows:
* Built-in support for <<mvc-ann-requestmapping-head-options,HTTP HEAD and HTTP OPTIONS>>. * Built-in support for <<mvc-ann-requestmapping-head-options,HTTP HEAD and HTTP OPTIONS>>.
* New `@GetMapping`, `@PostMapping`, `@PutMapping`, `@DeleteMapping`, and `@PatchMapping` * New `@GetMapping`, `@PostMapping`, `@PutMapping`, `@DeleteMapping`, and `@PatchMapping`
_composed annotations_ for `@RequestMapping`. _composed annotations_ for `@RequestMapping`.
** See <<mvc-ann-requestmapping-composed>> for details.
* New `@RequestScope`, `@SessionScope`, and `@ApplicationScope` _composed annotations_ for web scopes. * New `@RequestScope`, `@SessionScope`, and `@ApplicationScope` _composed annotations_ for web scopes.
* New `@RestControllerAdvice` annotation with combined `@ControllerAdvice` with `@ResponseBody` semantics. * New `@RestControllerAdvice` annotation with combined `@ControllerAdvice` with `@ResponseBody` semantics.
* `@ResponseStatus` is now supported at the class level and inherited by all methods. * `@ResponseStatus` is now supported at the class level and inherited by all methods.