Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing List of Integers to GET REST API

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.

like image 715
virsha Avatar asked Jan 15 '15 06:01

virsha


People also ask

Can we send list in GET request?

This sends a request to the specified URI using the GET verb, and converts the response body into the requested Java type. This works great for most classes, but it has a limitation; we can't send lists of objects.


1 Answers

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 :

  • http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1

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)]

like image 193
virsha Avatar answered Oct 14 '22 00:10

virsha