JSONAPI - 链接资源中自身和相关资源之间的区别
JSONAPI - Difference between self and related in a links resource
为什么下面的 JSONAPI 资源中的 self
and related
引用不同?他们不是指向同一个资源吗?去 /articles/1/relationships/tags
和 /articles/1/tags
有什么区别?
{
"links": {
"self": "/articles/1/relationships/tags",
"related": "/articles/1/tags"
},
"data": [
{ "type": "tags", "id": "2" },
{ "type": "tags", "id": "3" }
]
}
您可以在此处阅读相关内容:https://github.com/json-api/json-api/issues/508。
基本上,/articles/1/relationships/tags
响应将是表示 articles
和 tags
之间关系的对象。响应可能是这样的(您在问题中提出的内容):
{
"links": {
"self": "/articles/1/relationships/tags",
"related": "/articles/1/tags"
},
"data": [
{ "type": "tags", "id": "2" },
{ "type": "tags", "id": "3" }
]
}
此响应仅提供必要的数据(在主数据属性中 - 数据)来操纵关系,而不是与关系相关的资源。话虽这么说,如果你想 create 新关系,你会调用 /articles/1/relationships/tags
,add 一个新标签(基本上更新关系)到文章,阅读哪些标签属于文章(您只需要标识在服务器上搜索它们)或删除文章标签。
另一方面,调用 /articles/1/tags
将使用标记作为 主要数据 以及它们具有的所有其他属性(文章、relationships、links 和其他顶级属性,例如 include、强调文本, 链接 and/or jsonapi).
它们是不同的。这是我项目中的一个例子。
试试 Get http://localhost:3000/phone-numbers/1/relationships/contact
你会得到这样的回应:
{
"links": {
"self": "http://localhost:3000/phone-numbers/1/relationships/contact",
"related": "http://localhost:3000/phone-numbers/1/contact"
},
"data": {
"type": "contacts",
"id": "1"
}
}
您没有得到您可能想要检索的attributes
和relationships
。
然后
试试 Get http://localhost:3000/phone-numbers/1/contact
你会得到这样的回应:
{
"data": {
"id": "1",
"type": "contacts",
"links": {
"self": "http://localhost:3000/contacts/1"
},
"attributes": {
"name-first": "John",
"name-last": "Doe",
"email": "john.doe@boring.test",
"twitter": null
},
"relationships": {
"phone-numbers": {
"links": {
"self": "http://localhost:3000/contacts/1/relationships/phone-numbers",
"related": "http://localhost:3000/contacts/1/phone-numbers"
}
}
}
}
}
您可以看到您检索了所有您想要的信息,包括 attributes
和 relationships
。
但是您应该知道 relationships
可以用于某些目的。请阅读 http://jsonapi.org/format/#crud-updating-to-one-relationships 作为示例。
为什么下面的 JSONAPI 资源中的 self
and related
引用不同?他们不是指向同一个资源吗?去 /articles/1/relationships/tags
和 /articles/1/tags
有什么区别?
{
"links": {
"self": "/articles/1/relationships/tags",
"related": "/articles/1/tags"
},
"data": [
{ "type": "tags", "id": "2" },
{ "type": "tags", "id": "3" }
]
}
您可以在此处阅读相关内容:https://github.com/json-api/json-api/issues/508。
基本上,/articles/1/relationships/tags
响应将是表示 articles
和 tags
之间关系的对象。响应可能是这样的(您在问题中提出的内容):
{
"links": {
"self": "/articles/1/relationships/tags",
"related": "/articles/1/tags"
},
"data": [
{ "type": "tags", "id": "2" },
{ "type": "tags", "id": "3" }
]
}
此响应仅提供必要的数据(在主数据属性中 - 数据)来操纵关系,而不是与关系相关的资源。话虽这么说,如果你想 create 新关系,你会调用 /articles/1/relationships/tags
,add 一个新标签(基本上更新关系)到文章,阅读哪些标签属于文章(您只需要标识在服务器上搜索它们)或删除文章标签。
另一方面,调用 /articles/1/tags
将使用标记作为 主要数据 以及它们具有的所有其他属性(文章、relationships、links 和其他顶级属性,例如 include、强调文本, 链接 and/or jsonapi).
它们是不同的。这是我项目中的一个例子。
试试 Get http://localhost:3000/phone-numbers/1/relationships/contact
你会得到这样的回应:
{
"links": {
"self": "http://localhost:3000/phone-numbers/1/relationships/contact",
"related": "http://localhost:3000/phone-numbers/1/contact"
},
"data": {
"type": "contacts",
"id": "1"
}
}
您没有得到您可能想要检索的attributes
和relationships
。
然后
试试 Get http://localhost:3000/phone-numbers/1/contact
你会得到这样的回应:
{
"data": {
"id": "1",
"type": "contacts",
"links": {
"self": "http://localhost:3000/contacts/1"
},
"attributes": {
"name-first": "John",
"name-last": "Doe",
"email": "john.doe@boring.test",
"twitter": null
},
"relationships": {
"phone-numbers": {
"links": {
"self": "http://localhost:3000/contacts/1/relationships/phone-numbers",
"related": "http://localhost:3000/contacts/1/phone-numbers"
}
}
}
}
}
您可以看到您检索了所有您想要的信息,包括 attributes
和 relationships
。
但是您应该知道 relationships
可以用于某些目的。请阅读 http://jsonapi.org/format/#crud-updating-to-one-relationships 作为示例。