AntPathMatcher

使用demo

@Test
public void test01() {
    AntPathMatcher pathMatcher = new AntPathMatcher();

    boolean match1 = pathMatcher.match("/user/getUserById/{userId}", "/user/getUserById/3");
    Map<String, String> pathParamMap1 = pathMatcher.extractUriTemplateVariables("/user/getUserById/{userId}", "/user/getUserById/3");
    boolean match2 = pathMatcher.match("/user/getUserById/{userId}", "/user/getUserById");

    System.out.println(match1); // true
    System.out.println(pathParamMap1); // {userId=3}
    System.out.println(match2); // false
}

@Test
public void test02() {
    AntPathMatcher pathMatcher = new AntPathMatcher();

    boolean match1 = pathMatcher.match("/user/getUserById/*", "/user/getUserById/3");
    Map<String, String> pathParamMap1 = pathMatcher.extractUriTemplateVariables("/user/getUserById/{userId}", "/user/getUserById/3");
    boolean match11 = pathMatcher.match("/user/getUserById/**", "/user/getUserById/3");
    Map<String, String> pathParamMap11 = pathMatcher.extractUriTemplateVariables("/user/getUserById/**", "/user/getUserById/3");
    String s11 = pathMatcher.extractPathWithinPattern("/user/getUserById/**", "/user/getUserById/3");
    boolean match12 = pathMatcher.match("/user/getUserById/**", "/user/getUserById/a/b");
    String s12 = pathMatcher.extractPathWithinPattern("/user/getUserById/**", "/user/getUserById/a/b");
    boolean match13 = pathMatcher.match("/user/getUserById/*", "/user/getUserById/a/b");
    boolean match2 = pathMatcher.match("/user/getUserById/{userId}", "/user/getUserById");

    System.out.println(match1);  // true
    System.out.println(pathParamMap1); // {userId=3}
    System.out.println(match11); // true
    System.out.println(pathParamMap11); // {}
    System.out.println(s11); // 3
    System.out.println(match12); // true
    System.out.println(s12); // a/b
    System.out.println(match13); // false
    System.out.println(match2); // false
}

@Test
public void test03() {
    AntPathMatcher pathMatcher = new AntPathMatcher();

    boolean m1 = pathMatcher.match("/a/*/c", "/a/b/c");
    String s1 = pathMatcher.extractPathWithinPattern("/a/*/c", "/a/b/c");
    boolean m2 = pathMatcher.match("/a/*/c", "/a/b/bb/c");
    System.out.println(m1); // true
    System.out.println(s1); // b/c
    System.out.println(m2); // false

}

@Test
public void test04() {
    AntPathMatcher pathMatcher = new AntPathMatcher();

    boolean m1 = pathMatcher.match("/a/**", "global/a/b/c");
    boolean m2 = pathMatcher.match("/a/**", "a/b/c");
    boolean m3 = pathMatcher.match("a/**", "/a/b/c");
    boolean m4 = pathMatcher.match("a/**", "a/b/c");
    boolean m5 = pathMatcher.match("/a/**", "/a/b/c");
    System.out.println(m1); // false
    System.out.println(m2); // false
    System.out.println(m3); // false
    System.out.println(m4); // true
    System.out.println(m5); // true


}

ant匹配规则

原文:https://www.cnblogs.com/leftthen/p/5212221.html、

扩展:AntPathMatcher路径匹配器,Ant风格的URLhttps://blog.csdn.net/f641385712/article/details/118032869

AntPathMatcher如名使用的ant 的匹配规则,我们先看看吧.

  字符wildcard    描述

   ?         匹配一个字符

   *         匹配0个及以上字符

   **         匹配0个及以上目录directories

看几个官方的例子吧:

  com/t?st.jsp -             匹配: com/test.jsp  ,  com/tast.jsp  ,  com/txst.jsp
  com/*.jsp -                匹配: com文件夹下的全部.jsp文件
  com/**/test.jsp -           匹配: com文件夹和子文件夹下的全部.jsp文件,
  org/springframework/**/*.jsp     匹配: org/springframework文件夹和子文件夹下的全部.jsp文件
  org/**/servlet/bla.jsp -      匹配: org/springframework/servlet/bla.jsp  ,  org/springframework/testing/servlet/bla.jsp  ,  org/servlet/bla.jsp

PathMatcher接口

主要是判断是否匹配pattern,并解析出path中的参数

package org.springframework.util;

public interface PathMatcher {

    /**
     * 判断传入的path是否可以作为pattern使用
     */
    boolean isPattern(String path);

    /**
     * 使用pattern匹配path
     */
    boolean match(String pattern, String path);

    /**
     * 如名,是否开始部分匹配
     */
    boolean matchStart(String pattern, String path);

    /**
     * 提取path中匹配到的部分,如pattern(myroot/*.html),path(myroot/myfile.html),返回myfile.html
     */
    String extractPathWithinPattern(String pattern, String path);

    /**
     * 提取path中匹配到的部分,只是这边还需跟占位符配对为map,
     * 如pattern(/hotels/{hotel}),path(/hotels/1),解析出"hotel"->"1"
     */
    Map<String, String> extractUriTemplateVariables(String pattern, String path);

    /**
     * 提供比较器
     */
    Comparator<String> getPatternComparator(String path);

    /**
     * 合并pattern,pattern1然后pattern2
     */
    String combine(String pattern1, String pattern2);

}

AntPathMatcher使用示例

1、match 跟 matchStart 的差异

package org.springframework.util;
public class AntPathMatcherTests {
    @Test
    public void match() {
        // ...
        assertFalse(pathMatcher.match("/x/x/**/bla", "/x/x/x/"));
        // ...
    }
    @Test
    public void withMatchStart() {
        // ...
        assertTrue(pathMatcher.matchStart("/x/x/**/bla", "/x/x/x/"));
        // ...
    }
}

2、extractPathWithinPattern

package org.springframework.util;
public class AntPathMatcherTests {
    @Test
    public void extractPathWithinPattern() throws Exception {
        // ...
        assertEquals("", pathMatcher.extractPathWithinPattern("/docs/commit.html", "/docs/commit.html"));
        assertEquals("cvs/commit", pathMatcher.extractPathWithinPattern("/docs/*", "/docs/cvs/commit"));
        assertEquals("docs/cvs/commit", pathMatcher.extractPathWithinPattern("/d?cs/*", "/docs/cvs/commit"));
        // ...
    }
}

3、extractUriTemplateVariables

package org.springframework.util;
public class AntPathMatcherTests {
    @Test
    public void extractUriTemplateVariables() throws Exception {
        Map<String, String> result = pathMatcher.extractUriTemplateVariables("/hotels/{hotel}", "/hotels/1");
        assertEquals(Collections.singletonMap("hotel", "1"), result);
        // ...
        result = pathMatcher.extractUriTemplateVariables("/{page}.*", "/42.html");
        assertEquals(Collections.singletonMap("page", "42"), result);
        // ...
    }
    /**
     * SPR-7787
     */
    @Test
    public void extractUriTemplateVarsRegexQualifiers() {
        Map<String, String> result = pathMatcher.extractUriTemplateVariables(
                "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar",
                "com.example-sources-1.0.0.jar");
        assertEquals("com.example", result.get("symbolicName"));
        assertEquals("1.0.0", result.get("version"));
        // ...
    }
}

4、combine

package org.springframework.util;
public class AntPathMatcherTests {
    @Test
    public void combine() {
        // ...
        assertEquals("/hotels", pathMatcher.combine("/hotels", null));
        assertEquals("/hotels/booking", pathMatcher.combine("/hotels/*", "/booking"));
        assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "booking"));
        assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "/booking"));
        assertEquals("/hotels/booking", pathMatcher.combine("/hotels", "/booking"));
        assertEquals("/hotels/{hotel}", pathMatcher.combine("/hotels/*", "{hotel}"));
        assertEquals("/hotels/**/{hotel}", pathMatcher.combine("/hotels/**", "{hotel}"));
        assertEquals("/hotels/*/booking/{booking}", pathMatcher.combine("/hotels/*/booking", "{booking}"));
    }
}
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值