Spring中的路径匹配器AntPathMatcher详解

 更新时间:2023年09月25日 10:01:58   作者:骑个小蜗牛  
这篇文章主要介绍了Spring中的路径匹配器AntPathMatcher详解,Spring的PathMatcher路径匹配器接口,用于支持带通配符的资源路径匹配,本文提供了部分实现代码,需要的朋友可以参考下

PathMatcher接口

Spring的PathMatcher路径匹配器接口,用于支持带通配符的资源路径匹配。

使用场景

PathMatcher接口在Spring的许多场景下使用,比如:

  • PathMatchingResourcePatternResolver:资源扫描,启动时扫描并加载资源
  • AbstractUrlHandlerMapping:请求路径映射到 Controller
  • WebContentInterceptor:拦截器拦截路径分析

接口方法

方法描述
boolean isPattern(String path)判断路径是否是模式
boolean match(String pattern, String path)判断路径是否完全匹配
boolean matchStart(String pattern, String path)判断路径是否前缀匹配
前缀匹配的意思:路径能与模式的前面部分匹配,但模式可能还有后面多余部分
例如:/test能前缀匹配/test/{id}(但模式还有多余的/{id}部分未匹配)
String extractPathWithinPattern(String pattern, String path)得到模式匹配的部分值
该方法只返回路径的实际模式匹配部分
例如:myroot/*.html 匹配 myroot/myfile.html 路径,结果为 myfile.html
Map<String, String> extractUriTemplateVariables(String pattern, String path)提取路径中的路径参数值
Comparator<String> getPatternComparator(String path)得到一个排序比较器,用于对匹配到的所有路径进行排序
String combine(String pattern1, String pattern2)合并两个模式

AntPathMatcher类

AntPathMatcher是Spring为PathMatcher接口提供的默认实现,支持Ant风格的路径匹配。

匹配规则

AntPathMatcher支持的匹配规则:

规则描述
?匹配一个字符
*在一个路径段中匹配零个、一个或多个字符
**匹配零个或多个路径段,直到路径结束
{id}匹配一个路径段,并将该路径段的值作为变量id的变量值
{id:[a-z]+}匹配一个满足正则([a-z]+)路径段,并将该路径段的值作为变量id的变量值

举例:

    public static void main(String[] args) {
        AntPathMatcher matcher = new AntPathMatcher();
        // ?
        System.out.println(matcher.match("/test/a?c", "/test/abc"));// true
        // *
        System.out.println(matcher.match("/test/*", "/test/"));// true
        System.out.println(matcher.match("/test/*", "/test/aa"));// true
        System.out.println(matcher.match("/test/*.html", "/test/aa"));// false
        // **
        System.out.println(matcher.match("/test/**", "/test/"));// true
        System.out.println(matcher.match("/test/**", "/test/aa"));// true
        System.out.println(matcher.match("/test/**", "/test/aa/bb"));// true
        // {id}
        System.out.println(matcher.match("/test/{id}", "/test/111"));// true
        System.out.println(matcher.match("/test/a{id}", "/test/a111"));// true
        System.out.println(matcher.match("/test/{id}/aa", "/test/111/aa"));// true
        System.out.println(matcher.match("/test/{id}-{name}/aa", "/test/111-haha/aa"));// true
        // {id:[a-z]+}
        System.out.println(matcher.match("/test/{id:[a-z]+}", "/test/111"));// false
        System.out.println(matcher.match("/test/{id:[a-z]+}", "/test/abc"));// true
        System.out.println(matcher.match("/test/{id:\\w+}", "/test/1a_"));// true
        System.out.println(matcher.match("/test/{id:\\w+}", "/test/--"));// false
    }

一些不匹配情况原因:

模式路径原因
/user/aaa//user/aaa结束符不一致
/user/*//user/aaa结束符不一致

实际在Spring项目中使用的时候,你会发现:就算实际请求的结束符为 / ,但还是能匹配成功。这又是为什么呢?

两个关键属性:

  • useSuffixPatternMatch:设置是否使用后缀模式匹配,如“/user”是否匹配/user.*,默认是这种模式下,实际请求.后面加任何后缀,都会匹配到。如:实际请求“/user.html”能匹配上“/user”。
  • useTrailingSlashMatch:设置是否使用后缀路径模式匹配,如“/user”是否匹配“/user/”,默认是这种模式下,实际请求加、后缀,都会匹配到。如:实际请求“/user/”能匹配上“/user”。

关键源码:

PatternsRequestCondition类的getMatchingPattern方法

private String getMatchingPattern(String pattern, String lookupPath) {
		// 模式与路径相等,直接返回模式
		if (pattern.equals(lookupPath)) {
			return pattern;
		}
		// 如果使用后缀模式匹配,返回的模式会拼接上合适的后缀,如.html
		if (this.useSuffixPatternMatch) {
			if (!this.fileExtensions.isEmpty() && lookupPath.indexOf('.') != -1) {
				for (String extension : this.fileExtensions) {
					if (this.pathMatcher.match(pattern + extension, lookupPath)) {
						return pattern + extension;
					}
				}
			}
			else {
				boolean hasSuffix = pattern.indexOf('.') != -1;
				if (!hasSuffix && this.pathMatcher.match(pattern + ".*", lookupPath)) {
					return pattern + ".*";
				}
			}
		}
		if (this.pathMatcher.match(pattern, lookupPath)) {
			return pattern;
		}
		// 如果使用后缀路径模式匹配,返回的模式会拼接上/
		if (this.useTrailingSlashMatch) {
			if (!pattern.endsWith("/") && this.pathMatcher.match(pattern + "/", lookupPath)) {
				return pattern + "/";
			}
		}
		return null;
	}

因此,getMatchingPattern方法返回的模式再与请求路径进行模式匹配当然能匹配上了。

主要方法

1. isPattern

判断路径是否是模式。

只要路径中拥有 * 、 ? 、 {} ,则就是模式。

public boolean isPattern(@Nullable String path) {
    if (path == null) {
        return false;
    }
    boolean uriVar = false;
    for (int i = 0; i < path.length(); i++) {
        char c = path.charAt(i);
        if (c == '*' || c == '?') {
            return true;
        }
        if (c == '{') {
            uriVar = true;
            continue;
        }
        if (c == '}' && uriVar) {
            return true;
        }
    }
    return false;
}

示例:

    public static void main(String[] args) {
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.isPattern("/test/{id}"));// true
    }

2. match

判断路径是否完全匹配

public boolean match(String pattern, String path) {
    return doMatch(pattern, path, true, null);
}

示例:

    public static void main(String[] args) {
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.match("/test/*", "/test/111"));// true
        System.out.println(matcher.match("/test/**", "/test/111/222"));// true
        System.out.println(matcher.match("/test/{id}", "/test/111"));// true
        System.out.println(matcher.match("/test/{id}/aa", "/test/111/aa"));// true
        System.out.println(matcher.match("/test/{id}-{name}/aa", "/test/111-haha/aa"));// true
        System.out.println(matcher.match("/test/{id}-{name}/aa", "/test/111-/aa"));// true
    }

3. matchStart

判断路径是否前缀匹配

前缀匹配的意思:路径能与模式的前面部分匹配,但模式可能还有后面多余部分(可以理解为模式是否是以路径开头)

public boolean matchStart(String pattern, String path) {
    return doMatch(pattern, path, false, null);
}

示例:

    public static void main(String[] args) {
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.matchStart("/test/*", "/test"));// true
        System.out.println(matcher.matchStart("/test/aa/*", "/test"));// true
        System.out.println(matcher.matchStart("/test/{id}", "/test"));// true
        System.out.println(matcher.matchStart("/test/{id}-{name}/aa", "/test"));// true
        System.out.println(matcher.matchStart("/test/{id}", "/test/111/222"));// false
    }

4. extractPathWithinPattern

得到模式匹配的映射部分。找出通过*或者?匹配上的那一段路径及其后续路径。

示例:

    public static void main(String[] args) {
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.extractPathWithinPattern("/test/*", "/test"));//
        System.out.println(matcher.extractPathWithinPattern("/test/*", "/test/aa"));// aa
        System.out.println(matcher.extractPathWithinPattern("/test/**", "/test/aa/bb"));// aa/bb
        System.out.println(matcher.extractPathWithinPattern("/test/a?c/aa", "/test/abc/aa"));// abc/aa
        System.out.println(matcher.extractPathWithinPattern("/test/aa?c/aa/cc", "/test/abc/aa"));// abc/aa
    }

5. extractUriTemplateVariables

路径必须完全匹配(否则抛出异常),并提取路径中的路径参数值。

示例:

    public static void main(String[] args) {
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.extractPathWithinPattern("/test/*", "/test"));//
        System.out.println(matcher.extractPathWithinPattern("/test/*", "/test/aa"));// aa
        System.out.println(matcher.extractPathWithinPattern("/test/**", "/test/aa/bb"));// aa/bb
        System.out.println(matcher.extractPathWithinPattern("/test/a?c/aa", "/test/abc/aa"));// abc/aa
        System.out.println(matcher.extractPathWithinPattern("/test/aa?c/aa/cc", "/test/abc/aa"));// abc/aa
    }

6. getPatternComparator

得到一个排序比较器。

public Comparator<String> getPatternComparator(String path) {
		return new AntPatternComparator(path);
	}

7. combine

合并两个模式。 示例:

    public static void main(String[] args) {
        AntPathMatcher matcher = new AntPathMatcher();
        System.out.println(matcher.combine("/test/*", "/test/aa"));// /test/aa
        System.out.println(matcher.combine("/test/*", "/test/aa/bb"));// /test/test/aa/bb
        System.out.println(matcher.combine("/test/**", "/test/aa"));// /test/aa
        System.out.println(matcher.combine("/test/{id}", "/test/aa"));// /test/{id}/test/aa
    }

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

相关文章

  • java使用Socket类接收和发送数据

    java使用Socket类接收和发送数据

    Socket类是负责处理客户端通信的Java类。本文主要是介绍java使用Socket类接收和发送数据,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-10-10
  • Spring boot使用spring retry重试机制的方法示例

    Spring boot使用spring retry重试机制的方法示例

    这篇文章主要介绍了Spring boot使用spring retry重试机制的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • 在Spring Boot应用程序中使用Apache Kafka的方法步骤详解

    在Spring Boot应用程序中使用Apache Kafka的方法步骤详解

    这篇文章主要介绍了在Spring Boot应用程序中使用Apache Kafka的方法步骤详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-11-11
  • Java的Object类九个方法技巧

    Java的Object类九个方法技巧

    这篇文章主要介绍了Java的Object类九个方法技巧,Java的Object 类的完整路径是java.lang.Object ,是所有类的父类编译,下文相关资料,需要的朋友可以参考一下
    2022-04-04
  • SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法

    SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法

    这篇文章主要介绍了SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • Java对接ansible自动运维化平台方式

    Java对接ansible自动运维化平台方式

    这篇文章主要介绍了Java对接ansible自动运维化平台方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Spring依赖注入底层原理详解

    Spring依赖注入底层原理详解

    这篇文章主要介绍了Spring依赖注入底层原理详解,  依赖注入是一种设计模式,它将对象之间的依赖关系从代码中移除,并由容器来管理这些依赖关系,依赖注入的主要目的是降低代码的耦合度,使代码更加灵活和可维护,需要的朋友可以参考下
    2023-09-09
  • 解决常见的Eclipse SVN插件报错方法详解

    解决常见的Eclipse SVN插件报错方法详解

    本篇文章是对常见的Eclipse SVN插件报错方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Springboot发送邮件功能的实现详解

    Springboot发送邮件功能的实现详解

    电子邮件是—种用电子手段提供信息交换的通信方式,是互联网应用最广的服务。本文详细为大家介绍了SpringBoot实现发送电子邮件功能的示例代码,需要的可以参考一下
    2022-09-09
  • Spring MVC数据绑定概述及原理详解

    Spring MVC数据绑定概述及原理详解

    这篇文章主要介绍了Spring MVC数据绑定概述及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论