首页 > 编程语言 > 详细

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

时间:2020-03-13 13:50:30      阅读:90      评论:0      收藏:0      [点我收藏+]
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请求连接匹配器-工具类

原文:https://www.cnblogs.com/bevis-byf/p/12485811.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!