Question

I'm constructing a restful service which needs to accept any number of parameters instead of the one from the sample below.

Assuming the following service routine

@RequestMapping("/start/id/{id}", RequestMethod.GET)
public void startService(@PathVariable String id) {...}

there's a client implementation based on RestTemplate

restTemplate.getForObject("/start/id/{id}", null, id);

Question: But given that it might be thousands of ids, what restful approaches do I have to sending all parameters in one request using RestTemplate? I've seen the suggestions

  • Add request body to GET request - Doesn't seem possible with RestTemplate?
  • Use a separator in id, (e.g., id1|id2|....|idn) - Seems like a hack
  • PUT the parameters first, then issue a GET to reference the ids - Double requests, seems non-intuitive
  • Adding multiple URL parameters (?id=foo&id=bar&.....&id=foobar)

I know similar questions (calling-a-restful-service-with-many-parameters, how-to-create-rest-urls-without-verbs, can-you-build-a-truly-restful-service-that-takes-many-parameters) has been asked before but I found it hard to spot a satisfactory answer, or at least an answer based on RestTemplate.

Was it helpful?

Solution

You could use a query parameter for all the ids:

/search?ids=1,2,3,4,5,6,7

Then tokenize by the comma and do your search. Remember that you will be capped by the maximum URL length if you issue this as a GET query string parameter.

OTHER TIPS

I don't think any part of RESTful design states that your url structure should be able to resolve entire collections. Your first (request parameter) and third (url parameter) suggestions are likely the best.

I would recommend the first. I'm sure its allowed in resttemplate. Looking at the documentation you provided, just implement one of the post methods. Each of them takes the request as a parameter. Inside there, I'm sure there is some kind of implementation of a getRequestParameters() method you can use to parse json/xml from the request body containing your ids.

Or, even better, how are all of these id's related? Do they all have a common parent resource? If so, then you could (and probably should) do something like...

/commonparent/{parentId}

And then inside your request handler, query for all of the ids, and procede like normal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top