英文:
Looping rest api
问题
我有一个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"
}
// ...
]
}
有大约数千个带有nextPageLink
的页面。在最后一页,nextPageLink
为空。请指导我如何在Java中设计这个。此外,我还有另外10个不同的REST API要处理。
我的方法:
- 创建两个POJO类,一个用于
key
(KeyPojo
),另一个用于其余的数据(RestDataPojo
)。创建一个映射,以KeyPojo
作为键,RestDataPojo
作为值。 - 创建一个POJO类,其中包含所有的值,并将数据存储在类型为POJO的列表中。
是否有更好的方法来存储这种数据?这种方法是否足够高效?
英文:
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:
- 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.
- 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
得分: 4
'标准'解决这种问题的方法是根据RESTful Web Services Cookbook的建议,使用一个由关系类型(通常称为rel
)标记的链接数组。
一个典型的'页面'如下所示:
{
"links": [{
"rel": "previous",
"href": "http://example.com/pages/41"
},
{
"rel": "next",
"href": "http://example.com/pages/43"
}
],
"otherAttributes": "在此添加其他属性"
}
在第一页,您省略了previous
链接:
{
"links": [{
"rel": "next",
"href": "http://example.com/pages/43"
}
],
"otherAttributes": "在此添加其他属性"
}
在最后一页,您省略了next
链接:
{
"links": [{
"rel": "previous",
"href": "http://example.com/pages/41"
}
],
"otherAttributes": "在此添加其他属性"
}
由于links
是一个数组,您可以使用相同的底层DTO来处理所有三种情况。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论