问题描述
限时送ChatGPT账号..我正在组合一个基于 REST 的 API,但我不确定我应该如何为集合和单个资源提供响应.
I'm putting together a REST based API but I'm not sure on how I should deliver the response for collections vs individual resources.
在 REST 世界中对单个项目的集合进行精简表示有意义吗?
Does it make sense to have a slimmed down representation for a collection over a single item in the world of REST?
假设我有一些类似的专辑收藏:
Say I have something along the lines of this for a collection of albums:
{
items: [
{
"id": 1,
"title": "Thriller"
},
...
]
}
但是对于我拥有的实际单个项目
But then for the actual individual item I had
{
"id": 1,
"title": "Thriller",
"artist": "Michael Jackson",
"released": "1982",
"imageLinks": {
"smallThumbnail": "...",
"largeThumbnail": "..."
}
...
}
推荐答案
一个资源表示应该是唯一的,无论它是作为一个集合还是单个项目给出.但是,您可以引入一个像 fields 这样的新参数,客户端可以使用它来仅获取所需的字段,从而优化带宽.
A resource representation should be unique irrespective of whether it is given as a collection or a single item. But, you can introduce a new parameter like fields which can be used by the clients to get only the required field thereby optimising the bandwidth.
/albums
- 这应该给出对象列表,每个对象都具有您在单个项目 api 中提供的结构
/albums
- This should give the list of objects each having the structure of what you would give in a individual item api
/albums?fields=id,title
- 这可以给出只有 id & 的对象列表.标题.
/albums?fields=id,title
- This can give the list of objects with just the id & title.
这篇关于REST API 中集合与项目的部分表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论