Class PathPattern
java.lang.Object
org.springframework.web.util.pattern.PathPattern
- All Implemented Interfaces:
- Comparable<PathPattern>
Representation of a parsed path pattern. Includes a chain of path elements
for fast matching and accumulates computed state for quick comparison of
patterns.
PathPattern matches URL paths using the following rules:
- ?matches one character
- *matches zero or more characters within a path segment
- **matches zero or more path segments until the end of the path
- {spring}matches a path segment and captures it as a variable named "spring"
- {spring:[a-z]+}matches the regexp- [a-z]+against a path segment and captures it as a path variable named "spring"
- {*spring}matches zero or more path segments until the end of the path and captures it as a variable named "spring"
Note: In contrast to
AntPathMatcher, ** is supported only
at the end of a pattern. For example /pages/{**} is valid but
/pages/{**}/details is not. The same applies also to the capturing
variant {*spring}. The aim is to eliminate ambiguity when
comparing patterns for specificity.
Examples
- /pages/t?st.html— matches- /pages/test.htmlas well as- /pages/tXst.htmlbut not- /pages/toast.html
- /resources/*.png— matches all- .pngfiles in the- resourcesdirectory
- /resources/**— matches all files underneath the- /resources/path, including- /resources/image.pngand- /resources/css/spring.css
- /resources/{*path}— matches all files underneath the- /resources/, as well as- /resources, and captures their relative path in a variable named "path";- /resources/image.pngwill match with "path" → "/image.png", and- /resources/css/spring.csswill match with "path" → "/css/spring.css"
- /resources/{filename:\\w+}.datwill match- /resources/spring.datand assign the value- "spring"to the- filenamevariable
- Since:
- 5.0
- Author:
- Andy Clement, Rossen Stoyanchev
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classHolder for URI variables and path parameters (matrix variables) extracted based on the pattern for a given matched path.static classHolder for the result of a match on the start of a pattern.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final Comparator<PathPattern> Comparator that sorts patterns by specificity as follows: Null instances are last.
- 
Method SummaryModifier and TypeMethodDescriptioncombine(PathPattern otherPattern) Combine this pattern with the one given as parameter, returning a newPathPatterninstance that concatenates or merges both.intcompareTo(@Nullable PathPattern otherPattern) Compare this pattern with a supplied pattern: return -1,0,+1 if this pattern is more specific, the same or less specific than the supplied pattern.booleanDetermine the pattern-mapped part for the given path.Return the original String that was parsed to create this PathPattern.inthashCode()booleanWhether the pattern string contains pattern syntax that would require use ofmatches(PathContainer), or if it is a regular String that could be compared directly to others.matchAndExtract(PathContainer pathContainer) Match this pattern to the given URI path and return extracted URI template variables as well as path parameters (matrix variables).booleanmatches(PathContainer pathContainer) Whether this pattern matches the given path.matchStartOfPath(PathContainer pathContainer) Match the beginning of the given path and return the remaining portion not covered by this pattern.toString()
- 
Field Details- 
SPECIFICITY_COMPARATORComparator that sorts patterns by specificity as follows:- Null instances are last.
- Catch-all patterns are last.
- If both patterns are catch-all, consider the length (longer wins).
- Compare wildcard and captured variable count (lower wins).
- Consider length (longer wins)
 
 
- 
- 
Method Details- 
getPatternStringReturn the original String that was parsed to create this PathPattern.
- 
hasPatternSyntaxpublic boolean hasPatternSyntax()Whether the pattern string contains pattern syntax that would require use ofmatches(PathContainer), or if it is a regular String that could be compared directly to others.- Since:
- 5.2
 
- 
matchesWhether this pattern matches the given path.- Parameters:
- pathContainer- the candidate path to attempt to match against
- Returns:
- trueif the path matches this pattern
 
- 
matchAndExtractMatch this pattern to the given URI path and return extracted URI template variables as well as path parameters (matrix variables).- Parameters:
- pathContainer- the candidate path to attempt to match against
- Returns:
- info object with the extracted variables, or nullfor no match
 
- 
matchStartOfPathMatch the beginning of the given path and return the remaining portion not covered by this pattern. This is useful for matching nested routes where the path is matched incrementally at each level.- Parameters:
- pathContainer- the candidate path to attempt to match against
- Returns:
- info object with the match result or nullfor no match
 
- 
extractPathWithinPatternDetermine the pattern-mapped part for the given path.For example: - '/docs/cvs/commit.html' and '/docs/cvs/commit.html→ ''
- '/docs/*' and '/docs/cvs/commit' → 'cvs/commit'
- '/docs/cvs/*.html' and '/docs/cvs/commit.html→ 'commit.html'
- '/docs/**' and '/docs/cvs/commit→ 'cvs/commit'
 Notes: - Assumes that matches(PathContainer)returnstruefor the same path but does not enforce this.
- Duplicate occurrences of separators within the returned result are removed
- Leading and trailing separators are removed from the returned result
 - Parameters:
- path- a path that matches this pattern
- Returns:
- the subset of the path that is matched by pattern or "" if none of it is matched by pattern elements
 
- '
- 
compareToCompare this pattern with a supplied pattern: return -1,0,+1 if this pattern is more specific, the same or less specific than the supplied pattern. The aim is to sort more specific patterns first.- Specified by:
- compareToin interface- Comparable<PathPattern>
 
- 
combineCombine this pattern with the one given as parameter, returning a newPathPatterninstance that concatenates or merges both. This operation is not commutative, meaningpattern1.combine(pattern2)is not equal topattern2.combine(pattern1).Combining two "fixed" patterns effectively concatenates them: - "/projects" + "/spring-framework" => "/projects/spring-framework"
 - "/projects" + "/{project}" => "/projects/{project}"
 - "/projects/*" + "/spring-framework" => "/projects/spring-framework"
- "/projects/*.html" + "/spring-framework.html" => "/projects/spring-framework.html"
 - "/projects/**" + "/*.html" => "/projects/*.html"
- "/projects/*" + "/{project}" => "/projects/{project}"
- "/projects/*.html" + "/spring-framework.*" => "/projects/spring-framework.html"
 - "/projects/*/releases" + "/{id}" => "/projects/*/releases/{id}"
 - Parameters:
- otherPattern- the pattern to be combined with the current one
- Returns:
- the new PathPatternthat combines both patterns
- Throws:
- IllegalArgumentException- if the combination is not allowed
 
- 
equals
- 
hashCode
- 
toString
 
-