I just want to run my pagination method using Jersey on the server side and JQuery on the client.
On the server (assuming the user has just requested / left / users? Page = 1):
// Hardcoded the page number here, but it would be page-1, page+1 String prevLink = uriInfo.getRequestUri() + "?page=0"; String nextLink = uriInfo.getRequestUri() + "?page=2"; String linkHeader = String.format(LINK_HEADER_TMPL, prevLink, nextLink); return List<User> currentPageResults;
This gives me the following answer in response:
Link: <http://localhost:8880/rest/users?page=0>; rel="prev", <http://localhost:8880/rest/users?page=2>; rel="next"
On the client:
var xhr = $.getJSON("rest/users", function(allData) { var links = (xhr.getResponseHeader("link")).split(','); var relLinks = []; $.each(links, function(index, value) { var parts = value.split(';'); var url = parts[0].replace(/^.*<(.*)>.*$/m, '$1'); var rel = parts[1].replace(/^.*"(.*)".*$/m, '$1'); relLinks[rel] = url; }); alert(relLinks['prev']);
The warning gives me the correct information, and this is how I will use it to create links to pages:
http:
So the questions are:
- Is my link title correctly formatted? Parsing the client side link header seems a bit overwhelmed.
- Is this the correct / accepted way to do this? I looked at the Jersey response filter, but this requires adding Jersey-specific annotations to the model class, which I did not want to do.
- What about the common pages available? Is there a standardized way of giving this in information? If I had a shared page, could I supposedly completely remove the rel links and just generate them on the client side? Is it correct?
By the way, how did the whole format of link headers appear? The form "url; rel = name, url; rel = name" seems a little strange to me.
EDIT: Found this http://tools.ietf.org/html/rfc5988#section-6.2 that indicates the "standard" relationship names
source share