Looping rest api

Viewed 88

I've REST API which exposes the JSON with a link to the next page and data as shown below:

{
 "nextPageLink" : "rest_api_url_to_next_page"
 "myData" : [ {
   "key" : {
      "d1" : "d1_value",
      "d2" : "d2_value"
    },
    "productData" : {
       "d3" : "d3_value",
       "d4" : "d4_value"
    }
    "d5" : "d5_value",
    "d6" : "d6_value"
}

There are around 1000s of pages with nextPageLink. On the last page, its empty. Can you please guide me how to design this in java. Also, I've such 10 more different REST APIs to handle.

My approach:

  1. Create 2 pojos, one for key (keyPojo) and another for rest of the data(restDataPojo). Create a map with key as keyPojo and value as restDataPojo.
  2. Create a pojo which has all the values, and dump data in the list of type pojo.

Is there any better approach to store such data? is this approach efficient enough?

1 Answers

The 'standard' solution to this sort of problem recommended by the RESTful Web Services Cookbook is to have an array of links tagged by a relationship type (typically called rel).

A typical 'page' would look like this:

{
    "links": [{
            "rel": "previous",
            "href": "http://example.com/pages/41"
        },
        {
            "rel": "next",
            "href": "http://example.com/pages/43"
        }
    ],
    "otherAttributes": "go here"
}

On the first page, you omit the previous link:

{
    "links": [
        {
            "rel": "next",
            "href": "http://example.com/pages/43"
        }
    ],
    "otherAttributes": "go here"
}

and on the last page, you omit the next link:

{
    "links": [{
            "rel": "previous",
            "href": "http://example.com/pages/41"
        }
    ],
    "otherAttributes": "go here"
}

Since links is an array, you can back all three cases with the same underlying DTO.

Related