zoukankan      html  css  js  c++  java
  • SpringMVC 控制器映射 正则表达式

    当控制器方法映射为 @GetMapping("/path/to/something") 时, GET 请求 "/path/to/something.html" "/path/to/something.do" "/path/to/something.php" 都会被匹配到该方法上, 原因参见"内容协商与消息转换"

    如果要摆脱这种窘境, 需要使用正则表达式

    在SpringMVC中, 可以使用正则表达式来捕获路径变量(PathVariable), 格式如下:

    @RequestMapping("/path/to/{id:\d+}/{name:\w+$}")
    fun ...(@PathVariable id: Int, @PathVariable name: String) {
    ...
    

    如果不需要捕获变量, 可以不写变量名

    @RequestMapping("/path/to/something{:$}")
    fun ...
    

    这样一来, 就只有请求 "/path/to/something" 才会匹配该方法了

    • 注意: 不能使用分组
  • 相关阅读:
    Connected Graph
    Gerald and Giant Chess
    [NOI2009]诗人小G
    四边形不等式小结
    [NOI2007]货币兑换
    Cats Transport
    Cut the Sequence
    Fence
    The Battle of Chibi
    [Usaco2005 Dec]Cleaning Shifts
  • 原文地址:https://www.cnblogs.com/develon/p/11693886.html
Copyright © 2011-2022 走看看