我有一个带有 RestController 的 spring-boot(版本 2.2.5.RELEASE)应用程序。当我尝试使用最多 4 个 PathVariables 进行获取请求时,它正在工作。因此,以下代码会生成 200 OK 响应,正文中包含文本“test”。
@GetMapping(value = "/{a}/one/{b}/two/{c}/three/{d}/four")
public ResponseEntity<String> testParams(@PathVariable Long a, @PathVariable Long b, @PathVariable Long c, @PathVariable Long d) {
log.debug("TESTING");
return new ResponseEntity<String>("test", HttpStatus.OK);
}
但是,当我添加第 5 个 PathVariable 时,会返回回退(在本例中为 index.html 页面)。所以下面的代码似乎没有按预期工作。
@GetMapping(value = "/{a}/one/{b}/two/{c}/three/{d}/four/{e}/five")
public ResponseEntity<String> testParams(@PathVariable Long a, @PathVariable Long b, @PathVariable Long c, @PathVariable Long d,
@PathVariable Long e) {
log.debug("TESTING");
return new ResponseEntity<String>("test", HttpStatus.OK);
}
我似乎找不到任何关于此的信息。这是因为 PathVariables 的最大数量是 4 还是其他原因?我想现在我会将额外的参数添加为 RequestParam,因为这似乎确实有效。但是,如果有人知道这里发生了什么,我将非常感谢任何帮助。
编辑:正如多人指出的那样,PathVariables 的数量不应该是问题。所以问题可能出在项目的其他地方。由于这是一个非常大的应用程序,我无法发布所有可能相关的文件。如果其他人过去有类似的问题并在他们的项目中发现了问题,请分享。可能是同一个原因。