Wildcard Problem in SpringMVC Path Matching
- 2021-11-10 09:34:21
- OfStack
Wildcard Characters in SpringMVC Path Matching
The path specified in @ RequestMapping can also use the wildcard *
Represents any character. The following processor method can map requests/antstyle/a, can map requests/antstyle/b, but it cannot map request requests/antstyle/a/b because it can only map level 1 paths.
@RequestMapping("/antstyle/*")
public Object testAntStyle() {
return "antStyle";
}
Wildcard characters can't only be put at the end
It can also be placed in other places. In the following example, the wildcard character is placed in the middle, which can map requests/antstyle/a/bcd or requests/antstyle/ab/bcd.
@RequestMapping("/antstyle/*/bcd")
public Object testAntStyle() {
return "antStyle";
}
Wildcards can also match paths that end in a character
The following processor methods can map the path that ends with bcd in the/antstyle/post-path, such as/antstyle/abcd,/antstyle/bcd, and so on.
@RequestMapping("/antstyle/*bcd")
public Object testAntStyle() {
return "antStyle";
}
Wildcards can also match paths that start with a character
The following processor methods can map the/antstyle/abc, the/antstyle/abcd, and so on.
@RequestMapping("/antstyle/abc*")
public Object testAntStyle() {
return "antStyle";
}
One asterisk can only match a level 1 path, and two asterisks can be used if you need to match any multi-level path. The following processor methods can map any request whose request path begins with/antstyle/, such as/antstyle/a,/antstyle/a/b, and so on.
@RequestMapping("/antstyle/**")
public Object testAntStyle() {
return "antStyle";
}
One asterisk and two asterisks can also be used once. At this time, one asterisk can still match any character, but only at the current level, while two asterisks can still match any level, so it can match /antstyle/abca/xxx/xxx as follows.
@RequestMapping("/antstyle/abc*/**")
public Object testAntStyle() {
return "antStyle";
}
When wildcard characters are used in the request mapping path, path variables can also be used, which are independent of each other. In the following code, we use both path variables and wildcards in the request path.
@RequestMapping("/antstylewithpathvariable/*/{path}/abc")
public Object testAntStyleWithPathVariable(@PathVariable String path) {
return "ant style with path variable, path is " + path;
}
There is a priority relationship when path variables and wildcards match at the same time
When one request path can match more than one processor method, SpringMVC preferentially matches the more precise path mapping.
Path mapping with fewer path variables and wildcards is more accurate. For example, if /hotels/{hotel} /* has one path variable and one wildcard character, it will be more accurate than /hotels/{hotel} /**, which has one path variable and two wildcards. If the number of wildcards mapped by two paths is one, the path with more information specified will be more accurate, for example, /hotels/abc* is more accurate than /hotels/*. Path variables are more precise than wildcards. For example, /hotels/{hotel} is more accurate than /hotels/*. The default mapping/** has a lower priority than all other path mappings, such as/{a}/{b} being more accurate. A path map with two wildcards has a lower priority than any other path map without two wildcards, for example, /abc/** has a lower priority than /abc/{a}.(Note: This article is based on Spring 4.1. 0)
Request path parameters to use regular expressions
@GetMapping("/user/{id:\\d+}") // Use regular specification Id Is a number
public User getInfo(@PathVariable String id){
...
}