The @requestmapping of spring MVC

Source: Internet
Author: User

Introduction:

The rest style was used to develop the program in the previous time, but when the data was submitted in post and put, it was found that the server side did not accept the submitted data (the server-side parameter bindings did not add any annotations), and viewed the submission method as Application/json. And the server side of the data through Request.getreader () does exist in the browser submitted data. In order to find out the reason, the parameter binding (@RequestParam, @RequestBody, @RequestHeader, @PathVariable) was studied, and also looked at the relevant contents of Httpmessageconverter, summarized here.

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

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;

[Java]View Plaincopy
  1. @Controller
  2. @RequestMapping ("/appointments")
  3. Public class Appointmentscontroller {
  4. Private final AppointmentBook AppointmentBook;
  5. @Autowired
  6. Public Appointmentscontroller (AppointmentBook appointmentbook) {
  7. This.appointmentbook = AppointmentBook;
  8. }
  9. @RequestMapping (method = Requestmethod.get)
  10. Public map<string, appointment> get () {
  11. return Appointmentbook.getappointmentsfortoday ();
  12. }
  13. @RequestMapping (value="/{day}", method = Requestmethod.get)
  14. Public map<string, appointment> getforday (@PathVariable @DateTimeFormat (iso=iso. Date) (Date day, model model) {
  15. return Appointmentbook.getappointmentsforday (day);
  16. }
  17. @RequestMapping (value="/new", method = Requestmethod.get)
  18. Public Appointmentform Getnewform () {
  19. return new Appointmentform ();
  20. }
  21. @RequestMapping (method = Requestmethod.post)
  22. Public String Add (@Valid appointmentform appointment, bindingresult result) {
  23. if (result.haserrors ()) {
  24. return "Appointments/new";
  25. }
  26. Appointmentbook.addappointment (appointment);
  27. return "redirect:/appointments";
  28. }
  29. }

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)

[Java]View Plaincopy
    1. @RequestMapping (value="/owners/{ownerid}", Method=requestmethod.get)
    2. public string Findowner (@PathVariable string ownerid, model model) {
    3. Owner owner = ownerservice.findowner (ownerid);
    4. Model.addattribute ("owner", owner);
    5. return "Displayowner";
    6. }


Example C)

[Java]View Plaincopy
    1. @RequestMapping ("/spring-web/{symbolicname:[a-z-]+}-{version:\d\.\d\.\d}.{ Extension:\. [A-Z]} ")
    2. public void handle (@PathVariable string version, @PathVariable string extension) {
    3. // ...  
    4. }
    5. }



2 consumes, produces example

Examples of Cousumes:

[Java]View Plaincopy
    1. @Controller
    2. @RequestMapping (value = "/pets", method = Requestmethod.post, consumes="Application/json")
    3. Public void Addpet (@RequestBody pet Pet, model model) {
    4. //Implementation omitted
    5. }

The Content-type method only processes requests with the request "Application/json" type.

Examples of produces:

[Java]View Plaincopy
    1. @Controller
    2. @RequestMapping (value = "/pets/{petid}", method = Requestmethod.get, produces="Application/json")
    3. @ResponseBody
    4. Public Pet Getpet (@PathVariable String Petid, model model) {
    5. //Implementation omitted
    6. }

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:

[Java]View Plaincopy
  1. @Controller
  2. @RequestMapping ("/owners/{ownerid}")
  3. Public class Relativepathuritemplatecontroller {
  4. @RequestMapping (value = "/pets/{petid}", method = Requestmethod.get, params="Myparam=myvalue")
  5. public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {
  6. //Implementation omitted
  7. }
  8. }

A request with a value of "myvalue" named "Myparam" is included in the processing request only;

Examples of headers:

[Java]View Plaincopy
  1. @Controller
  2. @RequestMapping ("/owners/{ownerid}")
  3. Public class Relativepathuritemplatecontroller {
  4. @RequestMapping (value = "/pets", method = Requestmethod.get, headers="referer=http://www.ifeng.com/")
  5. public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {
  6. //Implementation omitted
  7. }
  8. }

The header that processes request only contains a request that specifies a "Refer" request header and a corresponding value of " http://www.ifeng.com/ ";

The above only describes which requests are processed by the method specified by Requestmapping, and the following article explains how to handle the data (data binding) and the data returned by request submission.

The @requestmapping of spring MVC

Related Article

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.