当控制器方法映射为 @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" 才会匹配该方法了

  • 注意: 不能使用分组

相关文章:

  • 2021-10-22
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-01
  • 2022-12-23
  • 2021-07-15
  • 2021-06-05
  • 2022-12-23
  • 2022-12-23
  • 2021-07-12
相关资源
相似解决方案