0

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.

Emmy
  • 3,493
  • 2
  • 16
  • 25
  • it's perfectly working check you are using correct url like `..../1/one/2/two/3/three/4/four/5/five` – Eklavya Jul 16 '20 at 15:39
  • 1
    I copied both methods into an empty project and did a request for each method. In both cases, the method was invoked with the given path variables. I can't reproduce your issue. Maybe it's related to something outside of this method. – Roland Weisleder Jul 16 '20 at 15:39
  • It's probably something else that's going on then, thanks for checking! Unfortunately it's a rather large and complex project that has this issue, so I'm not sure if I'll figure out what the actual problem is.. – Emmy Jul 17 '20 at 06:33

2 Answers2

0

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

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • This is not the issue. As I mentioned, the fallback is returned, which means the request mapping is not even found. If the PathVariable mappings where incorrect the request mapping would still be found, but you would get an error saying a PathVariable mapping is missing. – Emmy Jul 17 '20 at 06:45
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);
          }

This will work.

Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59
SAURABH
  • 283
  • 1
  • 3
  • 11
  • The problem is not the mapping of the PathVariables, nor is it the use of GetMapping instead of RequestMapping (trust me, i've tried all different ways of writing the same request mapping). If this code works in your project, the code I posted above most likely also works, so this is not the issue. It's probably something else in the project (I've edited the question to show this). – Emmy Jul 17 '20 at 06:52