是否允许仅返回 JSON API 集合的 ID?
Is returning only IDs for a JSON API collection allowed?
假设我有一个名为 articles
的资源。它们有一个数字 ID,您可以通过以下内容访问它们:
GET /articles/1
特定文章。
假设 return 类似于:
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints my bikeshed!",
"body": "A bunch of text here"
}
}
}
现在我的问题是如何处理对 GET /articles
的请求。 IE。如何处理对集合的请求。
您知道,访问文章正文既缓慢又痛苦。我希望 REST API 做的最后一件事实际上是尝试获取所有这些信息。然而,据我所知,JSON API 架构似乎假设您始终可以 return 全部资源。
是否有任何 "allowed" 方法来 return 只是 JSON API 下的 ID(或部分属性,如 "title"),同时主动不提供能否获得全部资源?
类似于:
GET /articles
returning:
{
"data": [
{
"type": "article_snubs",
"id": 1,
"attributes": {
"title": "JSON:API paints my bikeshed!"
}
}, {
"type": "article_snubs",
"id": 2,
"attributes": {
"title": "Some second thing here"
}
}
]
}
也许有完整文章的链接?
基本上,这在遵循 JSON API 或 REST 标准时是否完全可行?由于获取数据的相关成本,GET /articles
绝对不可能 return 使用全部资源,我认为这种情况并不少见。
JSON-API文档的"attributes"对象不需要是完整的表示:
attributes: an attributes object representing some of the resource’s data.
您可以提供 "self" link 来获得完整的表示,或者甚至可以提供 "body" link 来获得正文:
links: a links object containing links related to the resource.
例如
{
"data": [
{
"type": "articles_snubs",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"links": {
"self": "/articles/1",
"body": "/articles/1/body"
}
},
{
"type": "article_snubs",
"id": "2",
"attributes": {
"title": "Some second thing here"
},
"links": {
"self": "/articles/2",
"body": "/articles/2/body"
}
}
]
}
据我了解 JSON API 规范没有要求 API 必须 return 资源的所有字段(属性和关系)默认。我所知道的关于字段包含的唯一 MUST 语句与 Sparse Fieldsets(fields
查询参数)有关:
Sparse Fieldsets
[...]
If a client requests a restricted set of fields for a given resource type, an endpoint MUST NOT include additional fields in resource objects of that type in its response.
即便如此,规范并未禁止我不推荐这种方法。仅返回字段的子集会使使用 API 变得更加困难,因为您必须查阅文档才能获得所有受支持字段的列表。让客户决定应包含哪些信息(和related resources)更符合规范的含义。
假设我有一个名为 articles
的资源。它们有一个数字 ID,您可以通过以下内容访问它们:
GET /articles/1
特定文章。
假设 return 类似于:
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints my bikeshed!",
"body": "A bunch of text here"
}
}
}
现在我的问题是如何处理对 GET /articles
的请求。 IE。如何处理对集合的请求。
您知道,访问文章正文既缓慢又痛苦。我希望 REST API 做的最后一件事实际上是尝试获取所有这些信息。然而,据我所知,JSON API 架构似乎假设您始终可以 return 全部资源。
是否有任何 "allowed" 方法来 return 只是 JSON API 下的 ID(或部分属性,如 "title"),同时主动不提供能否获得全部资源?
类似于:
GET /articles
returning:
{
"data": [
{
"type": "article_snubs",
"id": 1,
"attributes": {
"title": "JSON:API paints my bikeshed!"
}
}, {
"type": "article_snubs",
"id": 2,
"attributes": {
"title": "Some second thing here"
}
}
]
}
也许有完整文章的链接?
基本上,这在遵循 JSON API 或 REST 标准时是否完全可行?由于获取数据的相关成本,GET /articles
绝对不可能 return 使用全部资源,我认为这种情况并不少见。
JSON-API文档的"attributes"对象不需要是完整的表示:
attributes: an attributes object representing some of the resource’s data.
您可以提供 "self" link 来获得完整的表示,或者甚至可以提供 "body" link 来获得正文:
links: a links object containing links related to the resource.
例如
{
"data": [
{
"type": "articles_snubs",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"links": {
"self": "/articles/1",
"body": "/articles/1/body"
}
},
{
"type": "article_snubs",
"id": "2",
"attributes": {
"title": "Some second thing here"
},
"links": {
"self": "/articles/2",
"body": "/articles/2/body"
}
}
]
}
据我了解 JSON API 规范没有要求 API 必须 return 资源的所有字段(属性和关系)默认。我所知道的关于字段包含的唯一 MUST 语句与 Sparse Fieldsets(fields
查询参数)有关:
Sparse Fieldsets
[...]
If a client requests a restricted set of fields for a given resource type, an endpoint MUST NOT include additional fields in resource objects of that type in its response.
即便如此,规范并未禁止我不推荐这种方法。仅返回字段的子集会使使用 API 变得更加困难,因为您必须查阅文档才能获得所有受支持字段的列表。让客户决定应包含哪些信息(和related resources)更符合规范的含义。