如何限制通过GET请求检索的数组元素数量(REST API JAVA)

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

How do i limit the number of array elements retrieved by a GET request (REST API JAVA)

问题

以下是已翻译的内容:

"Supposedly, My API retrieves JSON having around 100 ids and its respective values."
"我API应该检索到包含大约100个ID及其相应值的JSON。"

"How do i limit it to first 10 ids with its values?"
"我如何将其限制为前10个ID及其值?"

"Is there any JAVA code to limit this?"
"是否有任何JAVA代码来限制这个?"

英文:

Supposedly, My API retrieves JSON having around 100 ids and its respective values.

How do i limit it to first 10 ids with its values?

Is there any JAVA code to limit this?

[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},

{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
},

..... "id":100, ...

答案1

得分: 1

分页是您的最佳选择。因此,您可以向API调用传递一些查询参数,如限制和页数。例如,GET /users?limit=10&page=1 将获取前10个用户。如果您在后端使用Spring和数据库,Spring Data JPA已经内置了分页功能,使用起来相当容易。

英文:

Pagination is your best bet for this. So you could pass in some query params to the API call like limit and pageNumber. For example, GET /users?limit=10&page=1 will get you the first 10. If you are using Spring and a database on the backend. Spring Data JPA has pagination built in and it's fairly easy to use.

答案2

得分: 0

你可以使用分页来申请限制和页码。你可以像这样发起一个获取请求 => fetch(${url}/_limit=10&_page=1),这将获取到你所需的输出。

英文:

You can use pagination to apply for the limit and page number.
You can make a fetch request like => fetch(${url}/_limit=10&_page=1)
and this will fetch you your desired output

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

发表评论

匿名网友

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

确定