Overview:
PathMatcher is a conceptual model interface for Spring that abstractly models the concept "path matcher", a "path matcher" is a tool for path matching. Located in the Spring-core package under the util package.
PathMatcher interface source code
package ; import ; import ; public interface PathMatcher { /** * Does the given path represent a pattern that can be matched * by an implementation of this interface? * Determine whether the specified path path is a pattern (mode) * If the return value is false, that is, path is not a pattern, but a static path (real path string), * Then you don't need to call the method #match, because for static path matching, it is enough to directly use the string equal sign to compare. * @param path the path String to check * @return true if the given path represents a pattern */ boolean isPattern(String path); /** * Match the given path against the given pattern, * according to this PathMatcher's matching strategy. * Check whether the specified path path and the specified pattern match according to the current PathMatcher matching policy * @param The pattern used to detect whether the path string matches a certain pattern * @param path The path string that needs to be detected * @return true means match, false means mismatch */ boolean match(String pattern, String path); /** * Match the given path against the corresponding part of the given * pattern, according to this PathMatcher's matching strategy. * Check whether the specified path path and the specified pattern pattern are between the current PathMatcher matching policy. * Whether it is a prefix match * @param pattern the pattern to match against * @param path the path String to test * @return true means match, false means mismatch */ boolean matchStart(String pattern, String path); /** * Given a pattern and a full path, determine the pattern-mapped part. * Given a pattern and a full path path, determine the part of the path that matches the pattern. * * This method is supposed to find out which part of the path is matched * dynamically through an actual pattern, that is, it strips off a statically * defined leading path from the given full path, returning only the actually * pattern-matched part of the path. * This method is used to discover which part of the path is the part on which the pattern can dynamically match. It removes the static part of the path at the beginning, * Just return to that part that really matches the pattern. * Example: "myroot/*.html" is pattern, "myroot/" is path, * Then the method returns "". * The specific detection rules are based on the current PathMatcher matching strategy. * A simple implementation may return the given full path as-is in case * of an actual pattern, and the empty String in case of the pattern not * containing any dynamic parts (. the pattern parameter being * a static path that wouldn't qualify as an actual #isPattern pattern. * A sophisticated implementation will differentiate between the static parts * and the dynamic parts of the given path pattern. * @param pattern the path pattern * @param path the full path to introspect * @return the pattern-mapped part of the given path * (never null) */ String extractPathWithinPattern(String pattern, String path); /** * Given a pattern and a full path, extract the URI template variables. URI template * variables are expressed through curly brackets ('{' and '}'). * Given a pattern and a path, extract the URI template variable information. The URI template variable expression format is "{variable}" * * Example: pattern is "/hotels/{hotel}" and the path is "/hotels/1", then the method will return a map. * The content is: "hotel"->"1". * @param pattern the path pattern, possibly containing URI templates * @param path the full path to extract template variables from * @return a map, containing variable names as keys; variables values as values */ Map<String, String> extractUriTemplateVariables(String pattern, String path); /** * Given a full path, returns a Comparator suitable for sorting patterns * in order of explicitness for that path. * The full algorithm used depends on the underlying implementation, * but generally, the returned Comparator will sort a list so that more * specific patterns come before generic patterns. * @param path the full path to use for comparison * @return a comparator capable of sorting patterns in order of explicitness */ Comparator<String> getPatternComparator(String path); /** * Combines two patterns into a new pattern that is returned. * The full algorithm used for combining the two pattern depends on the underlying implementation. * Merge two modes. The specific merged algorithm is determined by the implementation class. * @param pattern1 the first pattern * @param pattern2 the second pattern * @return the combination of the two patterns * @throws IllegalArgumentException when the two patterns cannot be combined */ String combine(String pattern1, String pattern2); }
AntPathMatcher:
The Spring framework itself also provides a default implementation of AntPathMatcher for the concept model interface, which is used to match Ant-style paths. This is basically the path matching we usually use.
1. Wildcards:
? indicates that it matches any character
( ("/usr/name?", "/usr/name1") ); //true ( ("/usr/name?", "/usr/name111") ); //false
* means matching a single path
( ("/usr/*", "/usr/list")); //true ( ("/usr/*", "/usr/list/again")); //false ( ("/usr/*.app", "/usr/")); //true ( ("/usr/*.app", "/usr/")); //false
** means matching multi-level paths
( ("/usr/**", "/usr/list") ); //true ( ("/usr/**", "/usr/list/again") ); //true
Variable binding matching
( ("/usr/{name}/{age}", "/usr/Zhang San/12") ); //true ( ("/usr/{name}/{age}", "/usr/Zhang San") ); //false
Matching via extractPathWithinPattern() method
/** * extractPathWithinPattern Returns the path that matches the matching, removing the static part at the beginning of the path **/ ( ("/usr/*", "/usr/list") ); //list ( ("/usr/*/again", "/usr/list/again/age") ); // list/again/age ( ("/usr/*.html", "/usr/list") ); //list ( ("/usr/*.html", "/usr/") ); //
2. Variable binding
Variable binding allows us to extract parameters in the path during the matching process and assign them to the specified variable.
( ("/usr/{name}/{age}", "/usr/Zhang San/12") ); //{name=Zhang San, age=12}
This is the end of this article about the implementation of the PathMatcher path matcher in Spring. For more related contents of Spring PathMatcher path matcher, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!