- @RequestMapping Mapping Constraint requests
2.1. Map Request URL
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.
- At the class definition: Provides preliminary request mapping information. The root directory relative to the WEB app
- Method: Provides further subdivision mapping information. Relative to the URL at the class definition. If @RequestMapping is not marked at the class definition, the URL marked at the method is relative to the Web app's root directory
Dispatcherservlet intercepts the request, it determines the processing method corresponding to the request through the mapping information provided by @RequestMapping on the controller. A simple example is as follows:
2.2. Map request method, request header, request parameters
@RequestMapping can use request methods, request parameters, and request header mapping requests in addition to requesting 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, which is the relationship between them, and the combination of multiple conditions can make the request mapping more precise.
The 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
2.3. Support ant-style URL matching
ANT-style resource addresses support 3 matching characters:
- ?: matches one character in a file name
- *: matches any character in the file name
- **:** matching multi-layer paths
@RequestMapping also supports ANT-style URLs, examples:
/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.
The essence of @RequestMapping is to constrain HTTP request URLs, headers, parameters, methods, and so on.
"Springmvc" SPRINGMVC series 2 @requestmapping mapping constraint request