79

My requirement is as follows:

I want to give actor name, start date, end date and get all the films he acted in that period.

For that reason, my service request is like this.

  http://localhost:8080/MovieDB/GetJson?name=Actor&startDate=20120101&endDate=20120505

Now, i want to improve it. I want to give a start date, end date and more than one actor name and want to see all those actors movies in that period.

I am not sure how should my url look to support such thing.

I am writing a java based web service using spring.

Below code is to support one actor

   @RequestMapping(value = "/GetJson", method = RequestMethod.GET) 
    public void getJson(@RequestParam("name") String ticker, @RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
   //code to get results from db for those params.
 }

One solution i am thinking is using a % symbol to seperate actor names. For example:

 http://localhost:8080/MovieDB/GetJson?name=Actor1%Actor2%Actor3&startDate=20120101&endDate=20120505

Now, in the controller i will parse the name string with % and get back all actors names.

Is this a good way to do this or is there a standard approach?

Thanks

Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
javaMan
  • 6,352
  • 20
  • 67
  • 94
  • 2
    I think this is fine for 3 or 4 actors. But if you want to pass in 100 actors, maybe you can use POST and then pass a javascript object. The object can have name-value pairs for each actor. What do you think? Sorry for a sketchy reply – Victor Aug 09 '12 at 18:58

1 Answers1

173

Separate with commas:

http://localhost:8080/MovieDB/GetJson?name=Actor1,Actor2,Actor3&startDate=20120101&endDate=20120505

or:

http://localhost:8080/MovieDB/GetJson?name=Actor1&name=Actor2&name=Actor3&startDate=20120101&endDate=20120505

or:

http://localhost:8080/MovieDB/GetJson?name[0]=Actor1&name[1]=Actor2&name[2]=Actor3&startDate=20120101&endDate=20120505

Either way, your method signature needs to be:

@RequestMapping(value = "/GetJson", method = RequestMethod.GET) 
public void getJson(@RequestParam("name") String[] ticker, @RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
   //code to get results from db for those params.
 }
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Brian Dilley
  • 3,888
  • 2
  • 24
  • 24
  • 1
    but in case if u having 1000 size of array then u can not write this all the thing separate na.. ?? – Kishan Bheemajiyani Feb 11 '14 at 05:51
  • 4
    Is there a "Spring documentation" link you could point to regarding those 3 different ways of writing the URLs? – TPPZ Jul 03 '14 at 16:14
  • 30
    approach one (separate with commas) is different from the other two. Tried on a Java app (server side backed by undertow), if you pass `name=Actor1,Actor2,Actor3`, the server will not get an array for your `name` parameter, instead you get one single string: `Actor1,Actor2,Actor3` param. The other two is different, they all give `name` parameter with an array of values: `["Actor1", "Actor2", "Actor3"]`. According to http://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string, many other frameworks also treat approach one as a single string query param instead of array – Gelin Luo Jun 19 '16 at 09:43
  • 2
    with comma separated values you can receive them as a List request param – Qussay Najjar Jun 11 '18 at 14:38
  • For some reason, in Quarkus 2.10+, I sometimes can't map query parameters to a List using the short form. If I repeated the field name I could map the query parameters to a List every time, but only switching to a String[] allowed for the short comma separated format to work consistently. Leaving this here in case it helps someone. – TheFunk Jul 11 '22 at 19:22
  • 1
    Brackets [] seems to be forbidden in the form of option 3 in IPv4 URI address. – Ferdynand Kiepski Jul 19 '22 at 17:59