Spring Learning two @RequestMapping

Source: Internet
Author: User

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);

@RequestMapping (value={"/foo", "/bar"}). Multiple values can be specified

     @RequestMapping ("/appointments")   //If only one value can be omitted, value
  @RequestMapping ("/action3/{p1}/{p2}")  //path placeholder, supports regular expressions
Public String action3 (@PathVariable int p1, @PathVariable int p2,model Model) {       model.addattribute ("message", P1+p       2);       return "Foo/index";    }

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;

@Controller @requestmapping ("Home")  Public class HomeController {    //  request Content type must be text/html, note that the browser does not specify Content-type by default    @ Requestmapping (value = "/action8", consumes= "text/html")    public  String action8 (Model Model) {        Model.addattribute ("message", "Requested commit content type (Content-type) is text/html");         return "Foo/index";    }}

Produces: Specifies the type of content to return, only if the package contains the specified type in the request header (Accept) type ;

// The client receives JSON and is encoded as utf-8, and most browsers accept the */*, receiving any type    @RequestMapping (value = "/action9", produces= "Application/json; charset=utf-8")    public  String Action9 (model model) {        Model.addattribute ("message", "the type that the client can receive is Application/json; Charset=utf-8 ");         return "Foo/index";    }
3, Params,headers;

Params: Specifies that some parameter values must be included in the request before the method is processed.

  @RequestMapping (value = "/action10", params={"id=215", "Name!=abc"})    public  String ACTION10 (model) {        Model.addattribute ("message", "the requested parameter must contain id=215 and name not equal to ABC");         return "Foo/index";    }

Headers: Specifies that certain header values must be included in the request in order for the method to process requests.

  @RequestMapping (value = "/action11", headers= "host=localhost:8088")    public  String ACTION11 (model model) {        Model.addattribute ("message", "Request header information must contain host=localhost:8088");         return "Foo/index";    }

Spring Learning two @RequestMapping

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.