这里的真正问题是:您的第二个示例是否符合URI标准?URI标准规定,路径包含分层部分,查询包含非分层部分,但据我所知,它并没有说明如何在您的情况下设计URI结构。REST统一接口约束有一个HATEOAS部分,这意味着您应该发送指向上下级资源的链接,并使用元数据注释这些链接,以便客户端可以处理它们,从而了解链接的内容。因此,在实践中,URI结构并不重要...
翻译的内容:
GET /shows/123
{
"label": "The actual show",
"_embedded": {
"episodes": [
{
"label": "The first episode of the actual show",
"_embedded": {
"associations": [
]
},
"_links": {
"self": {
"label": "Link to the first episode of the actual show",
"href": "/episodes/12345"
},
"first": {
"href": "/episodes/12345"
},
"duplicate": {
"href": "/networks/3/shows/123/episodes/12345"
},
"up": {
"label": "Link to the actual show",
"href": "/shows/123"
},
"next": {
"label": "Link to the next episode of the actual show"
"href": "/episodes/12346"
},
"previous": null,
"last": {
"href": "/episodes/12350"
}
}
}
]
},
"_links": {
"self": {
"label": "Link to the actual show",
"href": "/shows/123"
},
"duplicate": {
"href": "/networks/3/shows/123"
},
"up": {
"label": "Link to the actual network",
"href": "/networks/3"
},
"collection": {
"label": "Link to the network tree",
"href": "/networks"
},
"next": {
"label": "Link to the next show in the actual network",
"href": "/shows/124"
},
"previous": {
"label": "Link to the previous show in the actual network",
"href": "/shows/122"
}
}
}
现在,这只是一个基于HAL+JSON和IANA链接关系的beta版示例。但是你也可以使用带有RDF词汇表(例如schema.org+hydra)的JSON-LD。这个示例仅涉及层次结构(上、第一个、下一个、上一个、最后一个、集合、项等),但你应该添加更多元数据,例如指向网络、节目和剧集的链接等。因此,客户端可以从元数据中了解内容,并且例如可以使用链接自动导航。这就是REST的工作原理,因此URI结构对客户端并不重要。(如果你想使响应更紧凑,也可以使用紧凑型URI和URI模板。)