其他分享
首页 > 其他分享> > SpringMVC请求连接匹配器-工具类

SpringMVC请求连接匹配器-工具类

作者:互联网

package PathMatchUtils;

import org.springframework.util.AntPathMatcher;

import java.util.Map;

/**
 * 请求地址匹配器
 * (1)? 匹配一个字符(除过操作系统默认的文件分隔符)
 * (2)* 匹配0个或多个字符
 * (3)**匹配0个或多个目录
 * (4){spring:[a-z]+} 将正则表达式[a-z]+匹配到的值,赋值给名为 spring 的路径变量.
 *     (PS:必须是完全匹配才行,在SpringMVC中只有完全匹配才会进入controller层的方法)
 */
public class PathMatchUtils {

    /**
     * @param pattern     <pre>/api/text    /api/t?xt   /api/*  /api/{pathVar}等等</pre>
     * @param servletPath 来自request.getServletPath()
     * @return
     */
    public static boolean match(String pattern, String servletPath) {
        final AntPathMatcher pathMatcher = new AntPathMatcher();
        return pathMatcher.match(pattern, servletPath);
    }

    /**
     * @param pattern     /api/{pathVar}
     * @param servletPath 来自request.getServletPath()
     * @return Map<String, String>  例如: {pathVar: index}
     */
    public static Map<String, String> groupTemplateVariables(String pattern, String servletPath) {
        final AntPathMatcher pathMatcher = new AntPathMatcher();
        Map<String, String> templateVariablesMap = pathMatcher.extractUriTemplateVariables(pattern, servletPath);
        return templateVariablesMap;
    }

}

 

标签:匹配,请求,SpringMVC,pattern,AntPathMatcher,api,servletPath,String
来源: https://www.cnblogs.com/bevis-byf/p/12485811.html