当控制器方法映射为 @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" 才会匹配该方法了
- 注意: 不能使用分组