GetMapping in spring-boot with 5 PathVariables not working

1.3k views Asked by At

I have a spring-boot (version 2.2.5.RELEASE) application with a RestController. When I try to do a get request with up to 4 PathVariables it's working. So the following code results in a 200 OK response with the text 'test' in the body.

 @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);
    }

However, when I add a 5th PathVariable the fallback is returned (in this case the index.html page). So the following code does not seem to work as expected.

   @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);
    }

I cannot seem to find any information on this. Is this because the maximum number of PathVariables is 4 or is something else going on? I guess for now I'll add the extra param as a RequestParam, as this does seem to work. However, if anyone knows what's going on here I would really appreciate any help.

EDIT: As multiple people have pointed out, the number of PathVariables shouldn't be the problem. So the problem probably lies elsewhere in the project. As this is a very big application I cannot post all files that might be related. If someone else had a similar problem in the past and found the issue in their project, please share. It might be the same cause.

2

There are 2 answers

1
SAURABH On
@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);
          }

This will work.

1
Giorgi Tsiklauri On

Try to use explicit mappings for the names of your path variables, like: @PathVariable("a") Long a and etc.