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