@RequestMapping usage

Source: Internet
Author: User

Introduction:

@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.

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;

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;

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:Value/method Example

Default Requestmapping ("...") that is value;

@Controller @RequestMapping ("/appointments")   Public classAppointmentscontroller {PrivateAppointmentBook AppointmentBook; @Autowired PublicAppointmentscontroller (AppointmentBook appointmentbook) { This. AppointmentBook =AppointmentBook; } @RequestMapping (Method=requestmethod.get) PublicMap<string, appointment>get () {returnAppointmentbook.getappointmentsfortoday (); } @RequestMapping (Value= "/{day}", method =requestmethod.get) PublicMap<string, appointment> getforday (@PathVariable @DateTimeFormat (iso=ISO. Date) (Date day, model model) {returnAppointmentbook.getappointmentsforday (day); } @RequestMapping (Value= "/new", method =requestmethod.get) Publicappointmentform Getnewform () {return NewAppointmentform (); } @RequestMapping (Method=requestmethod.post) PublicString 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) {    = Ownerservice.findowner (ownerid);      Model.addattribute ("owner", owner);       return "Displayowner";   }  
Example C)
@RequestMapping ("/spring-web/{symbolicname:[a-z-]+}-{version:\d\.\d\.\d}.{ Extension:\. [A-z]} " )    publicvoid  handle (@PathVariable string version, @PathVariable string extension) {          // ...     }  }  
consumes, produces example

Examples of Cousumes:

@Controller   = "/pets", method = Requestmethod.post, consumes= "Application/json")  publicvoid  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   = "/pets/{petid}", method = Requestmethod.get, produces= "Application/json")  @ResponseBody    public Pet getpet (@PathVariable String Petid, model model) {          //  implementation omitted< c10/>}  

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;

params, headers example

Examples of params:

@Controller  @RequestMapping ("/owners/{ownerid}")  publicclass  Relativepathuritemplatecontroller {      = "/pets/{petid}", method = Requestmethod.get, params= "Myparam=myvalue" )    publicvoid  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}")  publicclass  Relativepathuritemplatecontroller {    = "/pets", method = Requestmethod.get, headers= "referer=http:// www.ifeng.com/")    publicvoid  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/ ";

@RequestMapping usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.