• springBoot--web--路径匹配


    前言

    spring5.3之后加入了更多的请求路径匹配的实现策略
    以前只支持antPathMatcher策略,现在提供了PathPatternParse策略,并且可以让我们指定到底使用哪种策略
    PathPatternParser:
    在jmh基准测试下,有6-8倍的吞吐量提升,降低30%-40%空间分配律
    兼容AntPathMatcher语法,并支持更多类型路径模式
    **多段匹配的支持仅允许在模式末尾使用
    在这里插入图片描述

    在配置文件中配置

    在这里插入图片描述

    #改变路径匹配策略
    spring.mvc.pathmatch.matching-strategy=ant_path_matcher
    
    • 1
    • 2

    路径匹配结果

    1、Ant风格路径用法
    ant风格的路径模式语法具有以下规则:

    *:表示任意数量的字符
    ?:表示任意一个字符
    **:表示任意数量的目录
    {}:表示一个命名的模式占位符
    []:表示字符集合,列如:[a-z]表示小写字母
    例如:
    	*.html匹配任意名称,扩展名为.html的文件
    	/folder1/*/*.java 匹配在folder1目录下任意两级目录下的.java文件
    	/folder2/**/*.jsp 匹配在folder2目录下任意目录深度的.jsp文件
    	/(type)/[id].html 匹配任意文件名为[id].html,在任意命名的[type]目录下的文件
    注意:Ant风格的路径模式语法中的特殊字符需要转义,如:
    	要匹配文件路径中的星号,则需要转义为\\*
    	要匹配文件路径中的问好,则需要转义为\\?
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    package com.atguigu.boot304demo.controller;
    
    import jakarta.servlet.http.HttpServletRequest;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PatchMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author jitwxs
     * @date 2023年10月20日 10:55
     */
    @Slf4j
    @RestController
    public class HelloController {
        @GetMapping("/a*/b?/**/{p1:[a-f]+}/**")
        public String hello(HttpServletRequest request, @PathVariable("p1") String path){
            log.info("路径变量p1: {}", path);
            String url = request.getRequestURI();
            return url;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

  • 相关阅读:
    假期如何控制孩子视力?推荐视力康复师分享的护眼灯
    MATLAB初学者入门(8)—— 动态规划
    2022.6.28 Linux——线程安全
    scrapy框架选择器
    计算机毕业设计Java餐饮类网站(源码+系统+mysql数据库+lw文档)
    【游戏逆向】逆向基础之发包函数和线程发包
    Selenum八种常用定位(案例解析)
    Titanic 泰坦尼克号预测-Tensorflow 方法-【Kaggle 比赛】
    Pandas介绍
    Leetcode139. 单词拆分
  • 原文地址:https://blog.csdn.net/m0_50207524/article/details/133942469