Use wildcards in SpringMVC path matching
The path specified in @RequestMapping can also be used with wildcard characters*
Indicates any character. The following processor method can map request /antstyle/a, and can map request /antstyle/b, but it cannot map request /antstyle/a/b, because it can only map first-level paths.
@RequestMapping("/antstyle/*") public Object testAntStyle() { return "antStyle"; }
Wildcards are not only placed at the end
It can also be placed in other locations. In the following example, the wildcard 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 ending with a certain character
The following processor method can map paths ending with bcd by /antstyle/back paths, such as /antstyle/abcd, /antstyle/bcd, etc.
@RequestMapping("/antstyle/*bcd") public Object testAntStyle() { return "antStyle"; }
Wildcards can also match paths starting with a certain character
The following processor methods can map /antstyle/abc, /antstyle/abcd, etc.
@RequestMapping("/antstyle/abc*") public Object testAntStyle() { return "antStyle"; }
An asterisk can only match a first-level path, and if you need to match any multi-level path, you can use two asterisks. The following processor method can map any request whose request path starts with /antstyle/, such as /antstyle/a, /antstyle/a/b, etc.
@RequestMapping("/antstyle/**") public Object testAntStyle() { return "antStyle"; }
An asterisk and two asterisks can also be used together. At this time, an asterisk still matches any character, but it can only be at the current level, while two asterisks can still match any level, so the following can match /antstyle/abca/xxx/xxx, etc.
@RequestMapping("/antstyle/abc*/**") public Object testAntStyle() { return "antStyle"; }
When wildcards are used in the request map path, path variables can still 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 a request path can match multiple processor methods, SpringMVC will preferentially match the path map with more accurate ones.
- Path mapping with fewer path variables and wildcards is more accurate. For example, if /hotels/{hotel}/* has a path variable and a wildcard, it will be more accurate than /hotels/{hotel}/** and will match first, because the latter has a path variable and two wildcards.
- If the number of wildcards mapped by the two paths is the same, the path that has more information is more accurate, for example /hotels/abc* is more accurate than /hotels/*.
- Path variables are more accurate than wildcards. For example /hotels/{hotel} is more accurate than /hotels/*.
- The default map /** has lower priority than all other path maps, such as /{a}/{b}, which is more accurate than it.
- Path maps with two wildcards have lower priority than other path maps without two wildcards, such as /abc/** having lower priority than /abc/{a}.
(Note: This article is based on Spring 4.1.0)
Request path parameters using regular expressions
@GetMapping("/user/{id:\\d+}") //Use regular to specify the ID as a number public User getInfo(@PathVariable String id){ ... }
The above is personal experience. I hope you can give you a reference and I hope you can support me more.