@RequestMapping
RequestMapping is a process for annotation request address mapping, it can be used for class or method. For the class, all the methods in response to the request class are represented in the address as the parent path.
RequestMapping notes six attributes, let's put her into three categories described below.
value, method
value: Specifies the physical address of the request, the address may be specified URI Template mode (will be described later);
method: method specifies the type of request, GET, POST, PUT, DELETE and the like;
consumes,produces
consumes: submit a request specifying process of the content type (Content-Type), for example, application / json, text / html;
produces: content type of the returned request only if the request header (the Accept) containing the specified type will return to type;
params,headers
params: Specifies the request must contain certain parameter values, before allowing the processing method.
headers: Specifies the request must contain certain specified header value, in order for this method to process the request.
Example:
value / method example
Default RequestMapping ( ".... str ...") is the value of value;
@Controller @RequestMapping("/appointments") public class AppointmentsController { private AppointmentBook appointmentBook; @Autowired public AppointmentsController(AppointmentBook appointmentBook) { this.appointmentBook = appointmentBook; } @RequestMapping(method = RequestMethod.GET) public Map<String, Appointment> get() { return appointmentBook.getAppointmentsForToday(); } @RequestMapping(value="/{day}", method = RequestMethod.GET) public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) { return appointmentBook.getAppointmentsForDay(day); } @RequestMapping(value="/new", method = RequestMethod.GET) public AppointmentForm getNewForm() { return new AppointmentForm(); } @RequestMapping(method = RequestMethod.POST) public String add(@Valid AppointmentForm appointment, BindingResult result) { if (result.hasErrors()) { return "appointments/new"; } appointmentBook.addAppointment(appointment); return "redirect:/appointments"; } }
The uri value is the following three categories:
A) may be designated as a specific normal value;
B) may be specified as a class value (URI Template Patterns with Path Variables) comprising a variable;
C) can be specified as a class value having a regular expression (URI Template Patterns with Regular Expressions);
example B)
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET) public String findOwner(@PathVariable String ownerId, Model model) { Owner owner = ownerService.findOwner(ownerId); model.addAttribute("owner", owner); return "displayOwner"; }