@RequestMapping mapping requests, @PathVariable, @RequestParam, @RequestHeader use

Source: Internet
Author: User

1.@RequestMapping

Spring MVC uses @RequestMapping annotations to specify which URL requests can be processed by the controller, which can be labeled at the controller's class definition and method definition.

@RequestMapping

    • At the class definition: Provides preliminary request mapping information. Equivalent to the root directory of the current WEB app
    • Method: Provides further subdivision mapping information. Relative to the URL at the class definition.
    • If @RequestMappingis not marked at the class definition, the URL marked at the method is equivalent to the current WEB app's root directory
    • If the @RequestMappingis labeled at the class definition, the URL tagged at the method is relative to the @RequestMapping at the class definition!

Dispatcherservlet intercepts the request, it determines the processing method corresponding to the request through the mapping information provided by @RequestMapping on the controller.

Mapping request parameters, request methods, or request headers
@RequestMapping can use request methods, request parameters, and request header mapping requests in addition to request URL mapping requests

The value, method, params, and heads of the @RequestMapping respectively represent the request URL, request methods, request parameters and the mapping condition of the request header, and they are related to each other. Federated use of multiple conditions can make request mappings more precise.

(1) Params and headers support simple expressions:

    • PARAM1: Indicates that the request must contain a request parameter named param1
    • !PARAM1: Indicates that the request cannot contain a request parameter named param1
    • Param1! = value1: Indicates that the request contains a request parameter named param1, but its value cannot be value1
    • {"Param1=value1", "param2"}: The request must contain two request parameters named Param1 and param2, and the value of the param1 parameter must be value1!
1 @RequestMapping (value= "/helloparams", params={"username", "pwd!=123456"})2public  String helloparams () {3     return "Success"; 4 }

Indicates that the request URL must contain the username parameter, which PWD does not contain, and the value cannot be 123456 if the PWD is included.

(2) Ant-style request URL

ANT-style resource addresses support 3 matching characters:

    • ?: matches one character in a file name
    • *: matches any number of characters in the file name [except 0 characters!] ]
    • **:** matching multi-layer paths

For example:

    • /user/*/createuser: Match
      • URLs such as/user/aaa/createuser,/user/bbb/createuser, etc.
    • /user/**/createuser: Match
      • URLs such as/user/createuser,/user/aaa/bbb/createuser, etc.
    • /user/createuser??: Match
      • URLs such as/user/createuseraa,/USER/CREATEUSERBB, etc.

(3) placeholder for@PathVariable map URL Bindings
The URL with placeholders is a new feature of Spring3.0, a milestone in the development of SPRINGMVC to REST goals

By @PathVariable You can bind a placeholder parameter in a URL to an entry in the Controller processing method:

The {XXX} placeholder in the URL can be bound to the entry of an action method by a @PathVariable("xxx"), noting that the value of the note is consistent with the placeholder.

1 @RequestMapping (value= "/hellopathvariable/{id}")2publicthrows ioexception{3     System.out.println ("id=" +ID); 4     return "Success"; 5 }

(4) Method=requestmethod.get/post/put/delete, can implement the rest request style URL

1 //Rest Request Mode-----Get get2@RequestMapping (value= "/hello/{id}", method=requestmethod.get)3  PublicString Helloget (@PathVariable (value= "id") (Integer ID) {4     return"Success";5 }6 //Rest Request Mode-----Post Add7@RequestMapping (value= "/hello/{id}", method=requestmethod.post)8  PublicString Hellopost (@PathVariable (value= "id") (Integer ID) {9     return"Success";Ten } One //Rest Request Mode-----put modification A@RequestMapping (value= "/hello/{id}", method=requestmethod.put) -  PublicString Helloput (@PathVariable (value= "id") (Integer ID) { -     return"Success"; the } - //Rest Request Mode-----Delete Delete -@RequestMapping (value= "/hello/{id}", method=requestmethod.delete) -  PublicString Hellodelete (@PathVariable (value= "id") (Integer ID) { +     return"Success"; -}

How to use rest to request a style

(5)@RequestParam

@RequestParam can receive the requested parameters, equivalent to the GetParameter () method of the servlet!

Note: To separate the @requestparam and @pathvariable areas:

Three default properties:

    • Value: This field should match the value of the Name property of the request parameter!
    • Required: Boolean, the default is true, when specified as false, indicating that this parameter is not necessary, can not take!
    • DefaultValue: When we do not pass the value, the default is to use the value of DefaultValue, pass the parameter, use the parameter value we pass!
1 // GET request parameter information 2 @RequestMapping (value= "/helloreqparam")3 public String Helloreqparam (@ Requestparam (value= "username", required=false) String username) {4     System.out.println ("username-------" +username); 5     return SUCCESS; 6 }

(6)@RequestHeader

@RequestHeader: Gets the request header information, default properties:

    • Value: This field should match the value of the Name property of the request parameter!
    • Required: Boolean, the default is true, when specified as false, indicating that this parameter is not necessary, can not take!
    • DefaultValue: When we do not pass the value, the default is to use the value of DefaultValue, pass the parameter, use the parameter value we pass!
1 //GET request header information2@RequestMapping (value= "/helloreqheader")3  PublicString Helloreqheader (@RequestHeader (value= "Accept", required=true, defaultvalue= "123") (String accept) {4System.out.println ("Accept-------" +accept);5     //Accept-------text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.86     return"Success";7 }8@RequestMapping (value= "/helloreqheader")9  PublicString Helloreqheader (@RequestHeader (value= "Accept1", required=true, defaultvalue= "123") (String accept) {TenSystem.out.println ("Accept-------" +accept);//Accept-------123 One     return"Success"; A } -@RequestMapping (value= "/helloreqheader") -  PublicString Helloreqheader (@RequestHeader (value= "Accept1", required=true) (String accept) { theSystem.out.println ("Accept-------" +accept);//400 Error -     return"Success"; -}

@RequestMapping mapping requests, @PathVariable, @RequestParam, @RequestHeader use

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.