您的位置:首页 > 编程语言 > Java开发

Spring源码解析 - AntPathMatcher

2016-02-24 15:37 183 查看
最近在看SpringMVC的源码,发现request分发时,路径匹配最后是委托给AntPathMatcher实现的.索性看看吧.

文章摘要:

  1. ant匹配规则

  2. PathMatcher接口

  3. 通过测试用例看AntPathMatcher的使用

ant匹配规则

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}"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: