Passing List of Integers to GET REST API

52,424

Solution 1

I prefer a variant through HTTP path variable for your problem, because of in REST ideology a resource ID is passed after a resource name 'http://../resource/id' and HTTP parameters are used for filtering.

Through HTTP parameters

If you need to pass your ids through HTTP parameters, see an axample below:

Here is your Spring MVC controller method:

@RequestMapping(value = "/books", params = "ids", method = RequestMethod.GET)
@ResponseBody
Object getBooksById_params(@RequestParam List<Integer> ids) {
    return "ids=" + ids.toString();
}

And you can make a call using next variants:

  1. http://server:port/ctx/books?ids=5,65,42
  2. http://server:port/ctx/books?ids=5&ids=65&ids=42

Also take a look this discussion: https://stackoverflow.com/a/9547490/1881761


Through HTTP path variables

Also you can pass your ids through path variable, see an example below:

@RequestMapping(value = "/books/{ids}", method = RequestMethod.GET)
@ResponseBody
Object getBooksById_pathVariable(@PathVariable List<Integer> ids) {
    return "ids=" + ids.toString();
}

And your call will be look like this: http://server:port/ctx/books/5,65,42


Solution 2

Pros of GET HTTP call : It is always used for retrieval of Data.(From this perspective : we should implemented for each and every and retrieval)

Through HTTP parameters

If you need to pass your ids through HTTP parameters, see an axample below:

Here is your Spring MVC controller method:

@RequestMapping(value = "/book", params = "ids", method = RequestMethod.GET)
@ResponseBody
Object getBooksById_params(@RequestParam List<Integer> ids) {
return "ids=" + ids.toString();
}    

It works fine but for exceptional case : say URI is above 2048 characters. It means there are many Id's in the list(eg : 1000)

then its throws an exception : return 414 (Request-URI Too Long)

which is http://www.checkupdown.com/status/E414.html

After some research MY UNDERSTANDING is : The HTTP protocol does not place any a priori limit on the lenght of a URI. Servers MUST be able to handle the URI of any resources they serve, and SHOULD be able to handle URIs of unbounded length if they provide GET-based forms that could generate such URIs. A server SHOULD return 414(Request_URI Too Long) status if a URI is longer than the server can handle.

I have also gone through sites like to get the GET URI length :

So conclusion, is stick to POST call when there can be variable URI length at runtime for not getting such exception[return 414 (Request-URI Too Long)]

Share:
52,424
virsha
Author by

virsha

Learning is like daemon process of the human being.

Updated on April 22, 2021

Comments

  • virsha
    virsha about 3 years

    I wanted to fetch the List of Entities from database at Front end. So I have written POST REST HTTP call in Spring MVC.

    But I read the HTTP documentation which says whenever you have to retrieve data from database prefer GET call. So, Is it there is any I can replace the POST call to GET call from angular JS and pass list of Integers. But, GET HTTP has many drawbacks like : the length of URL is limited.Considering the case where we have to fetch 1000 entities from database.

    Please suggest me the possible way to get the entities or write GET REST API in Spring MVC for list of integers(refers to ID's of Entities).

    For Example : Consider there are 100 books in book table, But I want only few books, say id : 5,65,42,10,53,87,34,23. Thats why I am passing this List of Id's in a List of Integer in POST call.

    Currently stuck how to convert this to GET call. In Short, how to pass List of Integers through GET REST call.

  • virsha
    virsha over 9 years
    Thanks , I got your point. But I am stuck how to pass selected id's . For Example : Consider there are 100 books in book table, But I want only few books, say id : 5,65,42,10,53,87,34,23. Thats why I am passing this List of Id's in a List of Integer in POST call. Currently stuck how to convert this to GET call. In Short, how to pass List of Integers through GET call.
  • GameSalutes
    GameSalutes almost 8 years
    This is the correct answer IMO. stackoverflow.com/questions/4541338/… references that you must do tokenization yourself and accept as String but Spring handles the natural List case for you. Some people decide to use "POST" and pass in the List but that is not a true restful approach IMO. It needs to be a GET.
  • atul ranjan
    atul ranjan over 6 years
    the best way is avoid this situation, and handle this situation in backend, for eg: send the context of the situation: categories of the resources or etc
  • virsha
    virsha over 6 years
    Yeah true, we can avoid this situation. Hence, I have handled this in the backend. eg: booksCategories
  • Priyal
    Priyal over 6 years
    Regarding the path variable scenario, does this pattern supported by Jersey?