循环调用REST API

3

我有一个REST API,它公开了一个JSON,其中包含以下内容:下一页的链接和数据:

{
 "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"
}

有大约1000页带有nextPageLink的页面,最后一页为空。你能指导我如何在Java中设计它吗?此外,我还有10个不同的REST API需要处理。

我的方法:

  1. 创建2个POJO,一个用于键(keyPojo),另一个用于其余数据(restDataPojo)。创建一个以keyPojo为键、restDataPojo为值的映射。
  2. 创建一个包含所有值的POJO,并将数据倾入POJO类型的列表中。

是否有更好的方法来存储这样的数据?这种方法是否足够高效?

1个回答

4
这种问题的“标准”解决方案是由RESTful Web Services Cookbook推荐的,即使用一个由关系类型(通常称为rel)标记的链接数组。
一个典型的“页面”如下所示:
{
    "links": [{
            "rel": "previous",
            "href": "http://example.com/pages/41"
        },
        {
            "rel": "next",
            "href": "http://example.com/pages/43"
        }
    ],
    "otherAttributes": "go here"
}

在第一页,你省略了 previous 链接:
{
    "links": [
        {
            "rel": "next",
            "href": "http://example.com/pages/43"
        }
    ],
    "otherAttributes": "go here"
}

在最后一页上,你可以省略 next 链接。
{
    "links": [{
            "rel": "previous",
            "href": "http://example.com/pages/41"
        }
    ],
    "otherAttributes": "go here"
}

links是一个数组,你可以使用相同的基础数据传输对象来支持这三种情况。


感谢您提出这种方法。这真的很有帮助。 - tez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接