The requestmapping of Spring MVC

Source: Internet
Author: User

The first part, overview

/** mapping URLs to controller classes or handlers */
@Target ({elementtype.method, elementtype.type})
@Retention (Retentionpolicy.runtime)
@Documented
@Mapping
Public @interface requestmapping {

/**
* Specify the mapping name
* Support Controllers and methods
* When both controllers and methods are supported, use "#" to separate
*/
String name () default "";

/**
* Path Mapping
* The specified address can be a URI template, alias path, such as: @RequestMapping ("/foo"), @RequestMapping (path= "/foo")
* Support Controller and method, when it is used for controller mapping, all method mappings of the controller inherit this mapping
*/
@AliasFor ("path")
String[] Value () default {};

/**
* Only support servlet Runtime Environment: path mapping URIs (e.g. "/mypath.do")
* Support Ant-style path mode
* At the controller's method level, the relative path is supported (e.g. "edit.do")
* At the controller level, you can include placeholders (such as:/${connect})
* When it is used for controller mapping, all method mappings of the controller inherit this mapping
*/
@AliasFor ("value")
String[] Path () default {};

/**
* HTTP request mode (predicate type): GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
* Support Controller and method, when it is used for controller mapping, all method mappings of the controller inherit this mapping
*/
Requestmethod[] Method () default {};

/**
* Request parameter Qualification: Limit the request parameters sent by the client to some specific value or not to some value.
* How to use: "Myparam=myvalue", "Myparam!=myvalue"
* Support Controller and method, when it is used for controller mapping, all method mappings of the controller inherit this mapping
*/
String[] Params () default {};

/**
* Request Head
* How to use: "My-header=myvalue", "My-header!=myvalue"
* such as: requestmapping (value = "/something", headers = "content-type=text/*")
* Support Controller and method, when it is used for controller mapping, all method mappings of the controller inherit this mapping
*/
String[] Headers () default {};

/**
* Request Media Type (consumable media type) (text/html, Text/plain, Application/json;charset=utf-8, Application/octet-stream, etc.)
* For example: consumes = "Text/plain", consumes = {"Text/plain", "application/*"}
* Support Controller and method, when it is used for controller mapping, all method mappings of the controller override this mapping
*/
String[] Consumes () default {};

/**
* Response Media type (production type media type) (text/html, Text/plain, Application/json;charset=utf-8, Application/octet-stream, etc.)
* For example: consumes = "Text/plain", consumes = {"Text/plain", "application/*"}
* Support Controller and method, when it is used for controller mapping, all method mappings of the controller override this mapping
*/
String[] produces () default {};

}


Part II: PATH-related

1. Annotation method only

Default to Root Path

Access path: http://localhost:8080/auth/index.do

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16*/@Controllerpublic class Ssocontroller {//@Autowired//private Authservi  CE authservice; @RequestMapping (value = "/index.do", method = requestmethod.get) Public Modelandview Index () {Modelandview mv = new M    Odelandview ("login");  return MV; }}

  

2. Simultaneous annotation Controller and method

Overlay with Root path and method path

Access path: http://localhost:8080/auth/sso/index.do

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16*/@Controller @requestmapping ("/sso") public class Ssocontroller {    //@ autowired    //private authservice authservice;    @RequestMapping (value = "/index.do", method = requestmethod.get) public    Modelandview Index () {        Modelandview mv = New Modelandview ("login");        return mv;    }}    

  

3. Value is empty

The @requestmapping value on the controller is empty or not @requestmapping decorated, using the site root path
The value of @requestmapping on the method is null, using the controller root path
Above premise: Release default path on Web.,<url-pattern>/</url-pattern>

Access path: Http://localhost:8080/auth/sso

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16*/@Controllerpublic class Ssocontroller {    //@Autowired    //private Authservice Authservice;    @RequestMapping Public    Modelandview Index () {        Modelandview mv = new Modelandview ("login");        return mv;    }}        

  

4. Path variable placeholder
Use the value of the @pathvariable annotation method parameter to bind to a URI template variable

Access path: HTTP://LOCALHOST:8080/AUTH/SSO/INDEX/1

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16*/@Controller @requestmapping ("/sso") public class Ssocontroller {    //@ autowired    //private authservice authservice;    @RequestMapping ("/index/{id}") Public    Modelandview index (@PathVariable int id) {        modelandview mv = new Modelandview ("login");        return MV;}}

  

4.1) The path template can use regular expressions
such as: @RequestMapping (value= "/index/{id:\\d{3}}/{name:[a-z]{3}}")
Corresponding Access path: Http://localhost:8080/auth/sso/index/123/ass

4.2) Matrix Variable @matrixvariable
Matrix variables can appear in any path segment, with ";" For each matrix variable. Separated
Not used for the time being, use it again, the individual is not inclined to convey such complex things in the address

4.3) ant style path mode
such as/mypath/*.do,/mypath/*/auth/{uid}


Part III: Predicate types
Used to constrain the request type, including: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE, support controller and method, when it is used for controller mapping, all method mappings of the controller inherit this mapping. General identification method.

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16*/@Controller @requestmapping ("/sso") public class Ssocontroller {    //@ autowired    //private authservice authservice;    @RequestMapping (value= "/index.do", method= Requestmethod.post,requestmethod.get}) Public    Modelandview Index (@ pathvariable int ID, @PathVariable String name) {        Modelandview mv = new Modelandview ("login");        return mv;    }}

  

Part IV: Parameter qualification
The parameter that maps the request, which restricts the request parameters sent by the client to some specific value or not to some value.
Access path: HTTP://LOCALHOST:8080/AUTH/SSO/INDEX.DO?ID=1&NAME=BBB
Http://localhost:8080/auth/sso/index.do?id=1&name=aaa

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16*/@Controller @requestmapping ("/sso") public class Ssocontroller {    //@ autowired    //private authservice authservice;    @RequestMapping (value= "/index.do"        , Method={requestmethod.post,requestmethod.get}        , params={"id=1", "name !=AAA "}) public    Modelandview Index () {        Modelandview mv = new Modelandview (" login ");        return mv;    }}

  

Part V: Head limit
The head of the mapping request, which restricts the request header information sent by the client must contain some values or not for certain values.
Such as:

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16*/@Controller @requestmapping ("/sso") public class Ssocontroller {    //@ autowired    //private authservice authservice;    @RequestMapping (value= "/index.do"        , Method={requestmethod.post,requestmethod.get}        , params={"id=1", "name !=aaa "}        , headers=" host=localhost:8080 "    ) public    Modelandview Index () {        Modelandview mv = new Modelandview ("login");        return mv;    }}

  

Part VI: Content-type Limited
Request: @RequestMapping (value = "/action8", consumes= "Text/plain")
Response: @RequestMapping (value = "/action8", consumes= "text/html")

Such as:

/*** @ Function Rights Controller * @ author davee.yuan* @ Date 2017-01-16* @ description http://localhost:8080/auth/index*/@Controller @requestmapping ("/sso ") public class Ssocontroller {    //@Autowired    //private authservice authservice;    @RequestMapping (value= "/index.do"        , Method={requestmethod.post,requestmethod.get}        , params={"id=1", "name !=aaa "}        , headers=" host=localhost:8080 "       , produces=" text/html "    ) public    Modelandview Index () {        Modelandview mv = new Modelandview ("login");        return mv;    }}

  

Part VII: Other
Are you ready to use it?

The requestmapping of Spring MVC

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.