Spring中PathMatcher路径匹配器的实现

 更新时间:2024年07月11日 11:31:33   作者:涛哥是个大帅比  
Spring框架中的PathMatcher是一个接口,本文主要介绍了Spring中PathMatcher路径匹配器的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

概述:

PathMatcher是Spring的一个概念模型接口,该接口抽象建模了概念"路径匹配器",一个"路径匹配器"是一个用于路径匹配的工具。位于 Spring-core 包中 util 包下。

PathMatcher 接口源码

package org.springframework.util;
 
import java.util.Comparator;
import java.util.Map;
 
public interface PathMatcher {
 
    /**
     * Does the given path represent a pattern that can be matched
     * by an implementation of this interface?
     * 判断指定的路径 path 是否是一个 pattern(模式)
     * 如果返回值是 false,也就是说 path 不是一个模式,而是一个静态路径(真正的路径字符串),
     * 那么就不用调用方法 #match 了,因为对于静态路径的匹配,直接使用字符串等号比较就足够了。
     * @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.
     * 根据当前 PathMatcher 的匹配策略,检查指定的径 path 和指定的模式 pattern 是否匹配
     * @param 用于检测路径字符串是否匹配于某个模式时所用的模式
     * @param path 需要被检测的路径字符串
     * @return true 表示匹配, false 表示不匹配
     */
    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.
     * 根据当前 PathMatcher 的匹配策略,检查指定的径 path 和指定的模式 pattern 是否之间
     * 是否为前缀匹配
     * @param pattern the pattern to match against
     * @param path the path String to test
     * @return true 表示匹配, false 表示不匹配
     */
    boolean matchStart(String pattern, String path);
 
    /**
     * Given a pattern and a full path, determine the pattern-mapped part.
     * 给定一个模式 pattern 和一个全路径 path,判断路径中和模式匹配的部分。
     *
     * 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.
     * 该方法用于发现路径中的哪一部分是和模式能动态匹配上的部分。它会去除路径中开头静态部分,
     * 仅仅返回那部分真正和模式匹配的上的部分。
     * 例子 : "myroot/*.html" 为 pattern , "myroot/myfile.html" 为路径,
     *  则该方法返回 "myfile.html".  
     * 具体的检测规则根据当前 PathMatcher 的匹配策略来顶。    
     * 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 (i.e. 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 '}').
     * 给定一个模式和一个路径,提取其中的 URI 模板变量信息。URI模板变量表达式格式为 "{variable}"
     *      
     * 例子 : pattern  为 "/hotels/{hotel}" ,路径为 "/hotels/1", 则该方法会返回一个 map ,
     * 内容为 : "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.
     * 合并两个模式。具体合并的算法由实现类决定。
     * @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:

Spring框架自身对概念模型接口也提供了一个缺省的实现AntPathMatcher,用于匹配Ant风格的路径。我们平时使用的路径匹配基本用的都是这个。

1.通配符:

? 表示匹配任意一个字符

System.out.println( matcher.match("/usr/name?", "/usr/name1") );  //true
System.out.println( matcher.match("/usr/name?", "/usr/name111") );  //false

* 表示匹配单个路径

System.out.println( matcher.match("/usr/*", "/usr/list")); //true
System.out.println( matcher.match("/usr/*", "/usr/list/again")); //false
System.out.println( matcher.match("/usr/*.app", "/usr/list.app")); //true
System.out.println( matcher.match("/usr/*.app", "/usr/list.html")); //false

 ** 表示匹配多级路径

System.out.println( matcher.match("/usr/**", "/usr/list") );  //true
System.out.println( matcher.match("/usr/**", "/usr/list/again") );  //true

变量绑定匹配

System.out.println( matcher.match("/usr/{name}/{age}", "/usr/张三/12") );  //true
System.out.println( matcher.match("/usr/{name}/{age}", "/usr/张三") );  //false

 通过 extractPathWithinPattern() 方法匹配

/**
 * extractPathWithinPattern 返回符合匹配的部分路径,去除路径开头静态部分
 **/
System.out.println( matcher.extractPathWithinPattern("/usr/*", "/usr/list") );   //list
System.out.println( matcher.extractPathWithinPattern("/usr/*/again", "/usr/list/again/age") );   // list/again/age
System.out.println( matcher.extractPathWithinPattern("/usr/*.html", "/usr/list") );  //list
System.out.println( matcher.extractPathWithinPattern("/usr/*.html", "/usr/list.html") );  //list.html

2.变量绑定

变量绑定可以让我们在匹配过程中提取路径中的参数,并将其赋值给指定的变量。

System.out.println( matcher.extractUriTemplateVariables("/usr/{name}/{age}", "/usr/张三/12") ); //{name=张三, age=12}

到此这篇关于Spring中PathMatcher路径匹配器的实现的文章就介绍到这了,更多相关Spring PathMatcher路径匹配器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • SpringBoot 异步线程间数据传递的实现

    SpringBoot 异步线程间数据传递的实现

    本文主要介绍了SpringBoot 异步线程间数据传递的实现,包括异步线程的基本概念、数据传递的方式、具体实现方式等,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • SpringBoot基础教程之集成邮件服务

    SpringBoot基础教程之集成邮件服务

    这篇文章主要给大家介绍了关于SpringBoot基础教程之集成邮件服务的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用SpringBoot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • Springboot整合Dubbo+Nacos实现RPC调用的示例代码

    Springboot整合Dubbo+Nacos实现RPC调用的示例代码

    随着互联网技术的飞速发展,越来越多的企业和开发者开始关注微服务架构,Nacos是阿里巴巴开源的一个动态服务发现、配置管理和服务管理平台,本文讲解如何将Spring Boot与Dubbo和Nacos整合,实现RPC调用,需要的朋友可以参考下
    2024-02-02
  • MyBatis 与 Spring Data JPA 核心对比总结(选型指南与最佳实践)

    MyBatis 与 Spring Data JPA 核心对比总结(选型指南与最佳实践)

    MyBatis和Spring Data JPA是Java两大主流持久层框架,分别代表SQL可控性与面向对象开发效率的两种设计哲学,本文给大家介绍MyBatis与Spring Data JPA核心对比总结,感兴趣的朋友跟随小编一起看看吧
    2025-11-11
  • Spring之@Lookup注解详细解析

    Spring之@Lookup注解详细解析

    这篇文章主要介绍了Spring之@Lookup注解详细解析,当采用@Autowired注解对单例bean注依赖的原型bean时,会由于单例bean只会创建一次,导致依赖的原型bean也只会注入一次,@Lookup注解可以较为优雅的解决此类问题,需要的朋友可以参考下
    2024-01-01
  • 关于SpringMVC的异常处理机制详细解读

    关于SpringMVC的异常处理机制详细解读

    这篇文章主要介绍了关于SpringMVC的异常处理机制详细解读,SpringMVC是目前主流的Web MVC框架之一,本文将分析SpringMVC的异常处理内容,需要的朋友可以参考下
    2023-05-05
  • Java之注解@Data和@ToString(callSuper=true)解读

    Java之注解@Data和@ToString(callSuper=true)解读

    在使用Lombok库的@Data注解时,若子类未通过@ToString(callSuper=true)注明包含父类属性,toString()方法只打印子类属性,解决方法:1. 子类重写toString方法;2. 子类使用@Data和@ToString(callSuper=true),父类也应使用@Data
    2024-11-11
  • IDEA生成javadoc的实现步骤

    IDEA生成javadoc的实现步骤

    Javadoc是一种用于生成API文档的工具,它可以根据代码中特定格式的注释自动生成文档,本文主要介绍了IDEA生成javadoc的实现步骤,感兴趣的可以了解一下
    2023-10-10
  • java锁机制ReentrantLock源码实例分析

    java锁机制ReentrantLock源码实例分析

    这篇文章主要为大家介绍了java锁机制ReentrantLock源码实例分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • SpringBoot整合WxJava开启消息推送的实现

    SpringBoot整合WxJava开启消息推送的实现

    本文主要介绍了SpringBoot整合WxJava开启消息推送,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03

最新评论