0

我有一个带有 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 的数量不应该是问题。所以问题可能出在项目的其他地方。由于这是一个非常大的应用程序,我无法发布所有可能相关的文件。如果其他人过去有类似的问题并在他们的项目中发现了问题,请分享。可能是同一个原因。

4

2 回答 2

0
@RequestMapping(value ="/{one}/{two}/{three}/{four}/{five}/{six}",method=RequestMethod.GET)
public ResponseEntity<?> getData(@PathVariable("one") Long one, @PathVariable("two") Long two, @PathVariable("three") Long three, @PathVariable("four") Long four, @PathVariable("five") Long five, @PathVariable("six") Long six) {
           final String str = one + " "+ two +" "+ three +" "+ four+ " " + five + " "+ six;
           return ResponseEntity.ok(str);
          }

这将起作用。

于 2020-07-16T16:01:35.007 回答
0

尝试对路径变量的名称使用显式映射,例如:@PathVariable("a") Long a等。

于 2020-07-16T15:22:24.477 回答