Json api JSON API中的空稀疏字段集
我有一个资源(如帖子),它与其他资源(如评论)有着多对多的关系。我不需要相关资源的任何字段,只需要它们的自链接(按需异步获取它们)。响应应该如下所示:Json api JSON API中的空稀疏字段集,json-api,Json Api,我有一个资源(如帖子),它与其他资源(如评论)有着多对多的关系。我不需要相关资源的任何字段,只需要它们的自链接(按需异步获取它们)。响应应该如下所示: { "links": { "self": "http://example.com/posts/1?xxx", }, "data": [{ "type": "posts", "id": "1", "attributes": { "title": "JSON API paints my bike
{
"links": {
"self": "http://example.com/posts/1?xxx",
},
"data": [{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"comments": {
"links": {
"self": "http://example.com/posts/1/relationships/comments",
"related": "http://example.com/posts/1/comments"
},
"data": [
{ "type": "comments", "id": "5" },
{ "type": "comments", "id": "12" }
]
}
},
"links": {
"self": "http://example.com/posts/1"
}
}],
"included": [{
"type": "comments",
"id": "5",
"links": {
"self": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"links": {
"self": "http://example.com/comments/12"
}
}]
}
我的问题是请求的URL应该是什么样子
我的想法是包含注释,然后使用一个空的稀疏字段集来避免获得任何注释字段,而只获得自链接(+id,type)。
因此,它应该看起来像http://example.com/posts/1?include=comments&fields[注释]=[]
。
所以我需要一个空的稀疏字段集
JSON API规范没有太多说明稀疏字段集()及其与链接的关系。JSON API能够
简而言之,指定空稀疏字段集的正确方法是:
http://example.com/posts/1?include=comments&fields[评论]=
关于是否在关系对象中包含指向各个关系项的链接,存在一个问题:
{
"links": {
"self": "http://example.com/posts/1",
},
"data": [{
"type": "posts",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"comments": {
"links": {
"self": "http://example.com/posts/1/relationships/comments",
"related": "http://example.com/posts/1/comments",
"item": [ "http://example.com/comments/5", "http://example.com/comments/12" ]
},
"data": [{
"type": "comments",
"id": "5",
"links": {
"about": "http://example.com/comments/5"
}
}, {
"type": "comments",
"id": "12",
"links": {
"about": "http://example.com/comments/12"
}
}]
}
},
"links": {
"self": "http://example.com/posts/1"
}
}]
}