REST API中用于集合与项目的部分表示 [英] Partial Representations in REST API's for collections vs items
问题描述
我将基于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屋!