循环的 REST API

huangapple go评论72阅读模式
英文:

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要处理。

我的方法:

  1. 创建两个POJO类,一个用于keyKeyPojo),另一个用于其余的数据(RestDataPojo)。创建一个映射,以KeyPojo作为键,RestDataPojo作为值。
  2. 创建一个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:

  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

得分: 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.

huangapple
  • 本文由 发表于 2020年9月8日 10:21:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63786316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定