Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful Spring service with multiple parameters

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.

like image 470
Johan Sjöberg Avatar asked Dec 01 '25 17:12

Johan Sjöberg


1 Answers

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.

like image 193
Dave Avatar answered Dec 03 '25 09:12

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!