java - 如何知道 URL 是否与特定的 PATH 匹配
问题描述
我正在尝试在 Java 过滤器中执行一些逻辑,具体取决于请求 URL 是否与特定 PATH 匹配。我们想使用 Java 的 PathMatcher 之类的东西,而不是 Regex。另外要求,明确声明不使用 Spring,因此 spring 的 PathMatcher 不是一个选项。
例如给出以下 URL:
http://localhost:8080/base_path/customer/ {PARAM_VAL1}/order/{PARAM_VAL2}
我正在寻找某种方式使 IF 产生真实:
PathMatcher pm = new SomeURLPathMatcherImp("/base_path/customer/*/order/*");
myURI = "/base_path/customer/customer001/order/98536"
if ( pm. matches(myURI) ){
myFunction();
}
注意:我使用 PathMatcher 来描述问题。我想知道除了 Spring 之外是否有任何 API 库来执行类似于 Java 的 FileSystem 类实现的 URL PATH 匹配
解决方案
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:/base_path/customer/*/order/*");
System.out.println(pm.matches(Paths.get("/base_path/customer/customer001/order/98536"))); // true
System.out.println(pm.matches(Paths.get("/base_path/customer/customer001/order"))); // false
推荐阅读
- ios - UICollectionViewFlowLayout 中的 collectionView 委托“sizeForItemAt IndexPath”中的 EXC_BREAKPOINT 崩溃
- android - 是否可以停止 Spotify 或任何其他音乐应用程序的播放?
- reactjs - 在反应路由器dom中异步任务结束后仅重定向一次
- html - 导航栏固定位置收缩
- python - 插入后似乎自动删除的行
- netlink - 如何处理netlink重复事件
- python - 尝试使用 vs 代码在 venv 中创建夹层项目时无法解决导入问题
- php - 将多行合并为表中的单行
- angular - 我有角度的 cookieservice 问题
- rest - 带有正文 VS POST 的 GET 请求,用于检索大型资源