@RequestMapping
Requestmapping is an annotation that handles request address mappings and can be used on classes or methods. On a class, the method that represents all response requests in a class is the parent path of the address.
The requestmapping annotation has six properties, and we'll describe her in three categories below.
1, value, method;
Value: Specifies the actual address of the request, the specified address can be the URI Template mode (which will be explained later);
Method: Specifies the type of method requested, GET, POST, PUT, delete, and so on;
2, Consumes,produces;
Consumes: Specifies the type of submission to process the request (Content-type), such as Application/json, text/html;
Produces: Specifies the type of content returned, only if the specified type is included in the (Accept) type in the request header;
3, Params,headers;
Params: Specifies that some parameter values must be included in the request before the method is processed.
Headers: Specifies that certain header values must be included in the request in order for the method to process requests.
Example: 1, value/method example
Default Requestmapping ("...") that is value;
- @Controller
- @RequestMapping ("/appointments")
- Public class Appointmentscontroller {
- Private final 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 of value is the following three categories:
A) can be specified as a normal specific value;
B) can be specified as a class of values containing a variable (URI Template Patterns with Path Variables);
C) can be specified as a class of values with regular expressions (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";
- }
Example C)
- @RequestMapping ("/spring-web/{symbolicname:[a-z-]+}-{version:\d\.\d\.\d}.{ Extension:\. [A-Z]} ")
- public void handle (@PathVariable string version, @PathVariable string extension) {
- // ...
- }
- }
2 consumes, produces example
Examples of Cousumes:
- @Controller
- @RequestMapping (value = "/pets", method = Requestmethod.post, consumes="Application/json")
- Public void Addpet (@RequestBody pet Pet, model model) {
- //Implementation omitted
- }
The Content-type method only processes requests with the request "Application/json" type.
Examples of produces:
- @Controller
- @RequestMapping (value = "/pets/{petid}", method = Requestmethod.get, produces="Application/json")
- @ResponseBody
- Public Pet Getpet (@PathVariable String Petid, model model) {
- //Implementation omitted
- }
The Application/json method only handles requests in the request that accept headers contain "a", and implies that the returned content type is Application/json;
3 Params, headers example
Examples of params:
- @Controller
- @RequestMapping ("/owners/{ownerid}")
- Public class Relativepathuritemplatecontroller {
- @RequestMapping (value = "/pets/{petid}", method = Requestmethod.get, params="Myparam=myvalue")
- public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {
- //Implementation omitted
- }
- }
A request with a value of "myvalue" named "Myparam" is included in the processing request only;
Examples of headers:
- @Controller
- @RequestMapping ("/owners/{ownerid}")
- Public class Relativepathuritemplatecontroller {
- @RequestMapping (value = "/pets", method = Requestmethod.get, headers="referer=http://www.ifeng.com/")
- public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {
- //Implementation omitted
- }
- }
The header that processes request only contains a request that specifies a "Refer" request header and a corresponding value of " http://www.ifeng.com/ ";
"Turn" Spring MVC @requestmapping Detailed