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进行处理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 选择字符集 拉丁字符集_my
- 下一篇: springboot jar服务器运行后