Spring工具类--路径匹配(AntPathMatcher)--使用/实例

发布时间 2023-12-24 17:00:06作者: l_v_y_forever

原文网址:Spring工具类--路径匹配(AntPathMatcher)--使用/实例_IT利刃出鞘的博客-CSDN博客

简介

整个Spring(SpringBoot)框架的路径解析都是按照Ant的风格来的,比如:Controller的请求路径、文件路径、包的路径。所以,掌握Ant的路径匹配很重要。

Spring中的具体实现: org.springframework.util.AntPathMatcher。其注释里边有解释,翻译成中文如下表:

符号

作用

示例

?

匹配一个字符。

不能匹配目录:这个字符不能是代表路径分隔符的/

/dir/app?

匹配:/dir/app1、/dir/app2

不匹配:/dir/app、/dir/app12、index/

*

匹配0到多个字符。

/dir/app*

匹配:/dir/app、/dir/app1、/dir/app12、/dir/appa/

不匹配:/dir/app/a

**

匹配多级目录。

/dir/**/app*

匹配:/dir/xxx/app* /dir/xxx/yyy/app*

 

/dir/app/**

匹配:dir/app/aa/bcd/e

{spring:[a-z]+}

将正则表达式[a-z]+匹配到的值,赋值给名为 spring 的路径变量。

必须是完全匹配才行,在SpringMVC中只有完全匹配才会进入controller层的方法

@RequestMapping("/index/{username:[a-b]+}")
@ResponseBody
public String index(@PathVariable("username") String username){
    System.out.println(username);
    return username;
}

结果

index/ab           true  输出 ab
index/abbaaa    true  输出 abbaaa
index/a             false 404错误
index/ac            false 404错误

大全(有实例)

        摘录自Spring的官网测试,不得不说 Spring 的测试用例写的实在是太完善了。 其网址为:https://github.com/spring-projects/spring-framework/blob/main/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

        其测试太长了,本处将其分类。下边这行是公共代码:

private final AntPathMatcher pathMatcher = new AntPathMatcher();
 

match

  1.  
    // test exact matching
  2.  
    assertThat(pathMatcher.match("test", "test")).isTrue();
  3.  
    assertThat(pathMatcher.match("/test", "/test")).isTrue();
  4.  
    // SPR-14141
  5.  
    assertThat(pathMatcher.match("https://example.org", "https://example.org")).isTrue();
  6.  
    assertThat(pathMatcher.match("/test.jpg", "test.jpg")).isFalse();
  7.  
    assertThat(pathMatcher.match("test", "/test")).isFalse();
  8.  
    assertThat(pathMatcher.match("/test", "test")).isFalse();
  9.  
     
  10.  
    // test matching with ?'s
  11.  
    assertThat(pathMatcher.match("t?st", "test")).isTrue();
  12.  
    assertThat(pathMatcher.match("??st", "test")).isTrue();
  13.  
    assertThat(pathMatcher.match("tes?", "test")).isTrue();
  14.  
    assertThat(pathMatcher.match("te??", "test")).isTrue();
  15.  
    assertThat(pathMatcher.match("?es?", "test")).isTrue();
  16.  
    assertThat(pathMatcher.match("tes?", "tes")).isFalse();
  17.  
    assertThat(pathMatcher.match("tes?", "testt")).isFalse();
  18.  
    assertThat(pathMatcher.match("tes?", "tsst")).isFalse();
  19.  
     
  20.  
    // test matching with *'s
  21.  
    assertThat(pathMatcher.match("*", "test")).isTrue();
  22.  
    assertThat(pathMatcher.match("test*", "test")).isTrue();
  23.  
    assertThat(pathMatcher.match("test*", "testTest")).isTrue();
  24.  
    assertThat(pathMatcher.match("test/*", "test/Test")).isTrue();
  25.  
    assertThat(pathMatcher.match("test/*", "test/t")).isTrue();
  26.  
    assertThat(pathMatcher.match("test/*", "test/")).isTrue();
  27.  
    assertThat(pathMatcher.match("*test*", "AnothertestTest")).isTrue();
  28.  
    assertThat(pathMatcher.match("*test", "Anothertest")).isTrue();
  29.  
    assertThat(pathMatcher.match("*.*", "test.")).isTrue();
  30.  
    assertThat(pathMatcher.match("*.*", "test.test")).isTrue();
  31.  
    assertThat(pathMatcher.match("*.*", "test.test.test")).isTrue();
  32.  
    assertThat(pathMatcher.match("test*aaa", "testblaaaa")).isTrue();
  33.  
    assertThat(pathMatcher.match("test*", "tst")).isFalse();
  34.  
    assertThat(pathMatcher.match("test*", "tsttest")).isFalse();
  35.  
    assertThat(pathMatcher.match("test*", "test/")).isFalse();
  36.  
    assertThat(pathMatcher.match("test*", "test/t")).isFalse();
  37.  
    assertThat(pathMatcher.match("test/*", "test")).isFalse();
  38.  
    assertThat(pathMatcher.match("*test*", "tsttst")).isFalse();
  39.  
    assertThat(pathMatcher.match("*test", "tsttst")).isFalse();
  40.  
    assertThat(pathMatcher.match("*.*", "tsttst")).isFalse();
  41.  
    assertThat(pathMatcher.match("test*aaa", "test")).isFalse();
  42.  
    assertThat(pathMatcher.match("test*aaa", "testblaaab")).isFalse();
  43.  
     
  44.  
    // test matching with ?'s and /'s
  45.  
    assertThat(pathMatcher.match("/?", "/a")).isTrue();
  46.  
    assertThat(pathMatcher.match("/?/a", "/a/a")).isTrue();
  47.  
    assertThat(pathMatcher.match("/a/?", "/a/b")).isTrue();
  48.  
    assertThat(pathMatcher.match("/??/a", "/aa/a")).isTrue();
  49.  
    assertThat(pathMatcher.match("/a/??", "/a/bb")).isTrue();
  50.  
    assertThat(pathMatcher.match("/?", "/a")).isTrue();
  51.  
     
  52.  
    // test matching with **'s
  53.  
    assertThat(pathMatcher.match("/**", "/testing/testing")).isTrue();
  54.  
    assertThat(pathMatcher.match("/*/**", "/testing/testing")).isTrue();
  55.  
    assertThat(pathMatcher.match("/**/*", "/testing/testing")).isTrue();
  56.  
    assertThat(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla")).isTrue();
  57.  
    assertThat(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla")).isTrue();
  58.  
    assertThat(pathMatcher.match("/**/test", "/bla/bla/test")).isTrue();
  59.  
    assertThat(pathMatcher.match("/bla/**/**/bla", "/bla/bla/bla/bla/bla/bla")).isTrue();
  60.  
    assertThat(pathMatcher.match("/bla*bla/test", "/blaXXXbla/test")).isTrue();
  61.  
    assertThat(pathMatcher.match("/*bla/test", "/XXXbla/test")).isTrue();
  62.  
    assertThat(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test")).isFalse();
  63.  
    assertThat(pathMatcher.match("/*bla/test", "XXXblab/test")).isFalse();
  64.  
    assertThat(pathMatcher.match("/*bla/test", "XXXbl/test")).isFalse();
  65.  
     
  66.  
    assertThat(pathMatcher.match("/????", "/bala/bla")).isFalse();
  67.  
    assertThat(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb")).isFalse();
  68.  
     
  69.  
    assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
  70.  
    assertThat(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing")).isTrue();
  71.  
    assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
  72.  
    assertThat(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg")).isTrue();
  73.  
     
  74.  
    assertThat(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing/")).isTrue();
  75.  
    assertThat(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing")).isTrue();
  76.  
    assertThat(pathMatcher.match("*bla*/**/bla/**", "XXXblaXXXX/testing/testing/bla/testing/testing")).isTrue();
  77.  
    assertThat(pathMatcher.match("*bla*/**/bla/*", "XXXblaXXXX/testing/testing/bla/testing/testing")).isFalse();
  78.  
     
  79.  
    assertThat(pathMatcher.match("/x/x/**/bla", "/x/x/x/")).isFalse();
  80.  
     
  81.  
    assertThat(pathMatcher.match("/foo/bar/**", "/foo/bar")).isTrue();
  82.  
     
  83.  
    assertThat(pathMatcher.match("", "")).isTrue();
  84.  
     
  85.  
    assertThat(pathMatcher.match("/{bla}.*", "/testing.html")).isTrue();
  86.  
    assertThat(pathMatcher.match("/{bla}", "//x\ny")).isTrue();

matchWithNullPath

 

转载自:https://blog.csdn.net/feiying0canglang/article/details/120678900