1

my existing code is below:

@RequestMapping(value = "/{id}/{type}/{answer}", method = RequestMethod.GET)
@ResponseBody
public Results answer(@PathVariable("id") int questionId,
        @PathVariable int type,
        @PathVariable Boolean answer) {

        //do something
}

I want to be able to add the ability to make a request with an arbitrary number of path variables, but keeping the same pattern as above (i.e. /id/type/answer/id/type/answer/id/type/answer/...etc.).

So ideally I want to be able to create an API call that can support both of the following URLs:

http://www.example.com/sendAnswer/id1/typeA/0

AND

http://www.example.com/sendAnswer/id1/typeA/0/id2/typeB/1/id3/typeA/0

Does anyone have any ideas?

  • We just moved away from query string parameters. So we want to stay with path variables. And yes the only variable to have multiple values would be the answers path variable... we were going to do a comma delimited list for the answers. – user3461298 Mar 25 '14 at 19:44
  • so ideally I want to be able to create an API call that can support both of the following URLs: http://www.example.com/sendAnswer/id1/typeA/0 AND http://www.example.com/sendAnswer/id1/typeA/0/id2/typeB/1/id3/typeA/0 @ShaunScovil – user3461298 Mar 25 '14 at 19:47
  • I don't think it can be done with `@PathVariable` which uses `PathVariableMethodArgumentResolver` to resolve it's value. You could implement your own `ArgumentResolver` by extending it or subclass `AbstractNamedValueMethodArgumentResolver`. – Bart Mar 25 '14 at 20:20

1 Answers1

0

Based on your comments, consider creating two endpoints:

/{id}/{type}/{answerId} returns a specific answer.

/{id}/{type}/answers returns an array of all answers.

You really should be using query parameters to filter the results, in the case of retreiving multiple answers. Something like this:

/{id}/{type}/answers?ids=1,2,3&types=A,B,C

Shaun Scovil
  • 3,905
  • 5
  • 39
  • 58
  • Your two endpoints will be a horror to map properly. How can one decide which pattern to use? – Bart Mar 25 '14 at 20:22
  • @Bart Good catch. Meant to say `/{id}/{type}/answers` for the second endpoint. I've edited my answer accordingly. – Shaun Scovil Mar 26 '14 at 01:24