SpringMVC path rules, and use regular

@RequestMapping specified path can also use the wildcard * to represent any character. The processor may be mapped as follows request / antstyle / a, the request can be mapped / antstyle / b, but it can not be mapped Request / antstyle / a / b, since it can only maps a path.

@RequestMapping("/antstyle/*")
public Object testAntStyle() {
    return "antStyle";
}


Wildcards are not only placed last, it can also be placed in other locations. The following example is the wildcard in the middle position, it can be mapped request / antstyle / a / bcd, a mapping may request / antstyle / ab / bcd.

@RequestMapping("/antstyle/*/bcd")
public Object testAntStyle() {
    return "antStyle";
}


Wildcards can also match a certain character to the end of the path. The processor may map the following methods / antstyle / bcd path after the path to the end, such as / antstyle / abcd, / antstyle / bcd like.

@RequestMapping("/antstyle/*bcd")
public Object testAntStyle() {
    return "antStyle";
}


Wildcard matching can also be a path to start with the characters. The processor may map the following methods / antstyle / abc, / antstyle / abcd like.

@RequestMapping("/antstyle/abc*")
public Object testAntStyle() {
    return "antStyle";
}


An asterisk can only match a route, if you need to match any of the multi-stage path you can use two asterisks. The processor may map any method follows path start with a request / antstyle / request, such as / antstyle / a, / antstyle / a / b and the like.

@RequestMapping("/antstyle/**")
public Object testAntStyle() {
    return "antStyle";
}


An asterisk and two asterisks can also be used together, this time with an asterisk or match any character, but only at the current level, while two asterisks or can match any level, so as to match / antstyle / abca / xxx / xxx and so on.

@RequestMapping("/antstyle/abc*/**")
public Object testAntStyle() {
    return "antStyle";
}


Request a wildcard is used to map the path, the path is still available variables, which are independent of each other. In the following code we use variable path and request path wildcards.

@RequestMapping("/antstylewithpathvariable/*/{path}/abc")
public Object testAntStyleWithPathVariable(@PathVariable String path) {
    return "ant style with path variable, path is " + path;
}

Using regular

 @GetMapping(value = "/{prefix:[A-Z]+_[a-z]+}_{path:\\w+}{ext:\\.[a-z]+}")
    public String test(@PathVariable("prefix")String prefix, @PathVariable("path")String path,@PathVariable("ext")String ext){
        System.out.println(path);
        return path;
    }



Priority relationship exist when the variables and wildcard matching path
when a path request matches a plurality of processors methods, SpringMVC priority matching path map that more accurate.

The path has fewer variables and wildcard path mapping more accurate. For example / hotels / {hotel} / * path has a variable and a wildcard character, then it than / hotels / {hotel} / ** More precisely, preferentially match, because the latter has a variable path and two wildcards.
If the number of wildcard mapping of the two paths are the same, the more the information specified path will be more precise, such as / hotels / abc * ratio / hotels / * more accurate.
More accurate than path variable wildcard. For example / hotels / {hotel} / * is more accurate than / hotels.
Default mapping / ** priority over all other paths in the map are low, such as / {a} / {b} to be more accurate than it.
It has two wildcards priority than the other path mapping no wildcards two paths are mapped low, such as / abc / ** priority lower than / abc / {a}.

Guess you like

Origin blog.csdn.net/qq_39158142/article/details/90046851