• Springboot 使用升级小记-MVC path


    上文 Springboot 使用升级小记-循环依赖在升级 springboot 时遇到了循环依赖的问题,这次说说 mvc path 问题。

    升级到新的版本后,mvc path 匹配有变化,我们的老项目使用 springboot 与 jsp 的集成,也就是请求 url 都是遵循如 *.do *.json *.action 等形式的请求。那么升级都就出现 404 的错误。此时需要做如下配置 在 application.yaml:

    mvc:
      pathmatch:
        matching-strategy: ant_path_matcher
    
    • 1
    • 2
    • 3

    即让 mvc 对于 path 的解析匹配策略使用 ant_path_matcher 方式。

    为什么需要这么做呢?

    原因在于在 spring5 时,引入了信息 path 解析工具 PathPatternParser,低版本的 springboot 还是默认使用 AntPathMatcher 这个解析方式,高版本默认则使用 PathPatternParser。 所以通过设置使用 AntPathMatcher

    简介 PathPatternParser 与 AntPathMatcher

    Spring 5 引入了一个用于解析 URI 模板模式的新PathPatternParser。这是对先前所使用的 AntPathMatcher 的一个替代方案。

    AntPathMatcher 是实现 Ant 风格路径模式匹配的工具,而PathPatternParser 则将路径分解为一个由 PathElements 构成的链表。这一系列 PathElements 由 PathPattern 类接收,以快速匹配相应的模式。

    在 PathPatternParser 中,还引入了对新URI变量语法的支持。

    • 据说 PathPattern 性能比 AntPathMatcher 更好。
    • AntPathMatcher 使用范围广,可用于非 Web 环境,而 PathPattern 只适用于Web环境。
      所以感觉 PathPattern 并不是为了完全替代 AntPathMatcher 的。

    经过测试他们差别并不是很大,这是个人测试的使用 path 的方式,把常用的应该基本列全了,大家可以自己测试下,有问题欢迎讨论

    PathController

    @Controller
    public class PathController {
    
        @GetMapping("path1")
        public String path1() {
            return "path1";
        }
    
        @GetMapping("path2/index/*")
        public String path2() {
            return "path2";
        }
    
        @GetMapping("path3/index/**")
        public String path3() {
            return "path3";
        }
    
        @GetMapping("path4/*/path4")
        public String path4() {
            return "path4";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    RestPathController

    @RestController
    @RequestMapping("rest")
    public class RestPathController {
    
        @GetMapping("path1")
        public String path1() {
            return "path1";
        }
    
        @GetMapping("path2/index/*")
        public String path2() {
            return "path2";
        }
    
        /**
         * 末尾两个 ** 支持 path3/index 访问
         * @return
         */
        @GetMapping("path3/index/**")
        public String path3() {
            return "path3";
        }
    
        /**
         * 未使用 当个 * 正常。2* 个不行
         * @return
         */
        @GetMapping("path4/*/path4")
        public String path4() {
            return "path4";
        }
    
        /**
         *  ant_path_matcher 不支持如下的 url 访问
         *  非 ant_path_matcher 启动直接报错
         * @return
         */
    //    @GetMapping("path5/**/path5")
    //    public String path5() {
    //        return "path5";
    //    }
    
    
        @GetMapping("path6/{id}/{name}")
        public String path6(String id, String name) {
            return "path6: " + id + " : " + name;
    
        }
    
        @GetMapping("path7/{named:[a-z]+}")
        public String path7(String named) {
            return "path7: " + named;
        }
    
        @GetMapping("path8/{*named8}")
        public String path8(String named8) {
            return "path8: " + named8;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    Oracle(54)什么是本地索引(Local Index)?
    29【定时器和延时器】
    Prometheus+Grafana 部署
    vue脚手架创建项目:账号登录(利用element-ui快速开发)(取消eslint强制格式)(修改端口号)
    Quanergy欢迎Lori Sundberg出任首席人力资源官
    人工智能、机器学习和深度学习
    设计模式-观察者模式
    用友一面面经
    初级数值计算理论总结
    利用面向对象方法,处理数据文件【Python】
  • 原文地址:https://blog.csdn.net/ppblackboy/article/details/136251726