ant指定servlet版本_Spring工具 - AntPathMatcherUrlPathHelper(针对URL进行处理)
Spring工具篇(1)- AntPathMatcher&&UrlPathHelper(针对URL进行处理)
JAVA && Spring && SpringBoot2.x — 学习目录
源码版本:SpringBoot2.1.3 内含 Spring-5.1.5
源码位置:org.springframework.web.cors.UrlBasedCorsConfigurationSource#getCorsConfiguration
public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource {
//使用LinkedHashMap,保证url的顺序
private final MapcorsConfigurations = new LinkedHashMap<>();
private PathMatcher pathMatcher = new AntPathMatcher();
private UrlPathHelper urlPathHelper = new UrlPathHelper();
//方法
@Override
@Nullable
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
//获取URL的请求路径
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
for (Map.Entryentry : this.corsConfigurations.entrySet()) {
//进行Ant表达式匹配
if (this.pathMatcher.match(entry.getKey(), lookupPath)) {
return entry.getValue();
}
}
return null;
}
}
这个类的是CorsFilter的配置类,我们可以为每一个URL单独的提供CORS的配置。当请求时,根据Ant表达式选择最符合的配置。需要解决的问题:
如何获取Path路径;
如何使用Ant表达式进行匹配;
在Spring中提供了UrlPathHelper和AntPathMatcher工具类对URL进行匹配以及处理。
1. UrlPathHelper类
源码位置:org.springframework.web.util.UrlPathHelper
UrlPathHelper API官方文档
该类位于Spring web包下,若是基于Spring web开发的应用,可以使用该帮助类里面的方法。
该类是一个帮助类,用于UrlPath的获取。实际上,我们获取请求中的URL是通过Request对象的getContextPath、getServletPath、getRequestURI方法。
在SpringBoot2.x环境下:
修改web application的上下文路径。
server:
# 端口号
port: 8082
servlet:
# 应用上下文路径
context-path: /a
修改dispatchServlet(servlet-path)的映射路径
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
return new ServletRegistrationBean(dispatcherServlet,"/api/*");
}
若请求路径:http://localhost:8082/a/api/test/testool
方法
作用
结果
request.getContextPath()
web application路径
/a
getServletPath()
servlet的mapping路径
/api
getRequestURI()
url去除协议域名端口号后的路径
/a/api/testool
在UrlPathHelper中同时也含有获取request路径的方法:
方法名
作用
结果
getContextPath
获取web application路径
/a
getPathWithinApplication
获取web application内的路径
/api/testool
getServletPath
获取servlet-mapping路径
/api
getPathWithinServletMapping
获取Servlet-mapping映射内的路径
/testool
getLookupPathForRequest
根据alwaysUseFullPath属性,获取getPathWithinApplication或者getPathWithinServletMapping路径
1.alwaysUseFullPath=true则/api/testool;2. 否则/testool
getRequestUri
获取请求的uri地址
/a/api/testool
getOriginatingRequestUri
获取转发请求的源地址,若无转发地址,即为本身
/a/api/testool
注:若是servlet-mapping=/,则为默认的Servlet容器,此时的ServletPath为uri - contextPath。
一般而言,是调用getLookupPathForRequest方法,获取用户上送地址。
2. AntPathMatcher类
2.1 match方法(url匹配)
我们在UrlPathHelper可以获取到请求地址,而后我们可以对URLs字符串进行匹配。
SpringMVC的路径匹配规则使用Ant规则,其基本规则如下:
? 匹配一个字符;
* 匹配0个或多个字符;
** 匹配0个多多个目录;
使用方法:
@RequestMapping("antPath")
public void getPath(HttpServletRequest request){
UrlPathHelper urlPathHelper = new UrlPathHelper();
String path = urlPathHelper.getLookupPathForRequest(request);
System.out.println("path路径:"+path);
AntPathMatcher matcher=new AntPathMatcher();
boolean match = matcher.match("/**", path);
System.out.println("是否匹配:"+match);
}
2.2 combine(url的合并)
[科目版嗯]用于含有Ant路径的合并。
1. 规则如下表所示:
模式1
模式2
结果
null
null
null
/hotels
null
/hotels
null
/hotels
/hotels
/hotels
/bookings
/hotels/bookings
/hotels
bookings
/hotels/bookings
/hotels/*
/bookings
/hotels/bookings
/hotels/**
/bookings
/hotels/**/bookings
/hotels
{hotel}
/hotels/{hotel}
/hotels/*
{hotel}
/hotels/{hotel}
/hotels/**
{hotel}
/hotels/**/{hotel}
/*.html
/hotels.html
/hotels.html
/*.html
/hotels
/hotels.html
/*.html
/*.txt
IllegalArgumentException
2. 使用方式:
/* @param pattern1 the first pattern 模式1
* @param pattern2 the second pattern 模式2
* @return the combination of the two patterns 返回结果
* @throws IllegalArgumentException if the two patterns cannot be combined 两个模式无法合并
*/
@Override
public String combine(String pattern1, String pattern2) {
}
ant指定servlet版本_Spring工具 - AntPathMatcherUrlPathHelper(针对URL进行处理)相关推荐
- ant指定servlet版本_[转载]程序开发常见错误
[转载]程序开发常见错误 (2011-06-19 14:28:13) 标签: 转载 谢谢 项目中遇到的错误 sql错误 找不到列 Unknown column 'fillMen' in 'field ...
- ant指定servlet版本_阅读SpringMVC源码前,不妨看下简易版本SpringMVC框架的搭建
开发环境:windows10.idea.jdk1.8.apache-tomcat-9.0.0.M3 SpringMVC框架是基于Servlet设计的,所以如果你知道SpringMVC,但是没听过道Se ...
- linux安装指定mysql版本安装,linux yum安装指定版本mysql
1.下载mysql rpm包 cd /usr/local/src wget https://dev.mysql.com/get/mysql80-community-release-el7-.noarc ...
- VSTS 离线源码版本辅助工具
应用背景: 公司采用VSTS管理开发,但未对公网开放服务,在现场开发和更新版本都需要人为的将其找出并发给公司做构建:如此操作非常繁琐,并且容易弄错版本:针对这些情况,编写了一个版本检查工具:它 ...
- gradle 指定springcloud 版本_如何优雅地使用Gradle
Gradle是一个基于Apache Ant和Apache Maven概念的JVM项目自动化建构工具. 有别于传统的不忍卒读的XML项目设置语言,它使用基于Groovy或者Kotlin的DSL(领域专用 ...
- 【Android NDK 开发】Android NDK 下载 ( 下载指定历史版本 NDK | Android NDK r10e - 2015 年 5 月 )
文章目录 一.下载指定历史版本 NDK 一.下载指定历史版本 NDK 进入到 ndk 下载的 " 修订历史记录 " 页面 https://developer.android.goo ...
- 简单3步成功给Mac安装指定Python版本和指定库版本并且随意切换版本,非常简单,不行拿砖砸我
Mac安装Python真的头疼,我前后斗了一年. Mac默认安装2.7,我们平时2018年都用Python3了. 我按照网友的方法brew install python3,导致Python2和Pyth ...
- 12306订票助手文件版本同步工具
using System; using System.Collections.Generic; using System.Text; using System.Linq;namespace Ticke ...
- git获取指定release版本代码
首先手里必须有release的版本的备份出来的/.repo/manifests/default.xml文件,该文件记录了每个git库的在该版本下的具体的版本情况,整个代码的sync都是依据他来的: 1 ...
最新文章
- [hihocoder 1075] 开锁魔法III
- 使IIS服务器支持下载 apk/ipa 安装包
- 直立车各环的调试_平衡小车调试指南(直立环 速度环)
- 2021中青杯数学建模C题 在线教学的分析与研究
- php网站怎么缓存,一个php网站缓存代码分享
- 第一阶段:Java基础之控制结构
- Java多线程之Callable、Future和FutureTask
- Redis学习之复制(三)
- 安装分布式文件系统MooseFS
- mysql root_mysql root 没有任何权限
- C语言函数调用的方式求两个数的大小
- 高通9008刷机,刷机参考
- 软件测试技术+HR面试
- 在计算机上如何打开资源管理器,资源管理器怎么打开?打开资源管理器的5种方法...
- APP运行时Crash自动修复系统
- 【OptiX】第6个示例 折射,玻璃材质
- 图表嵌入到数据表格下方_如何在excel图表下方添加数据表 如何在excel图表中显示数值...
- 极大似然原理和贝叶斯分类器
- :after/::after和:before/::before的区别
- java读取微信消息_微信公众平台开发中使用Java如何实现获取用户的信息
热门文章
- java 增量,Java增量运算符查询(++ i和i ++)
- 如何查看linux内核版本?(发行版本)
- source insight(SI)C/C++符号标记含义
- ValueError: This model has not yet been built. Build the model first by calling build() or calling f
- activemq启动wrapper stopped
- html文件头部固定代码
- java 线程中创建线程_java – 在线程中创建线程 – 良好的做​​法?
- pb 如何判断缺纸_如何快速判断是否低估?四种相对估值法应用精析
- dotween曲线运动 unity_Unity中DOTween插件的DOTweenPath轨迹移动
- razer inc - hidclass_《原神》× Razer雷蛇典藏版礼盒套装首发 高颜值限量!