【发布时间】:2014-10-07 11:52:55
【问题描述】:
在 RESTful Web API 书中,作者建议公开配置文件并使用承认链接关系的内容类型。由Hydra 扩展的JSON-LD 似乎符合这些要求,我想在我的新API 设计中使用它们。
我目前遇到性能问题。假设我有一家在线自行车商店,我想检索有关给定自行车车轮的信息。
对于 Hydra 规范,在我看来,我需要发送 2 个请求来获取有关车轮的详细信息。 第一个请求是针对自行车本身:
GET /mybike HTTP/1.1
Host: wowbike.com
响应包含指向车轮集合的 Hydra::Link:
HTTP/1.1 200 OK
Content-Type: application/ld+json
{
"@context" :
{
"Bike": "/contexts/vocab#Bike"
},
"@id" : "/mybike",
"@type" : "Bike",
"size" : "L",
"wheels" : "/mybike/wheels" // "wheels" is a "hydra:Link"
}
现在我可以向wheels 资源发送第二个请求以获取详细信息:
GET /mybike/wheels HTTP/1.1
Host: wowbike.com
HTTP/1.1 200 OK
Content-Type: application/ld+json
{
"@context":
{
"Collection": "http://www.w3.org/ns/hydra/core#Collection",
"Wheel" : "/contexts/vocab#Wheel"
},
"@type" : "Collection",
"@id" : "/mybike/wheels",
"member" :
[
{
"@id" : "/mybike/wheels/firstwheel",
"@type" : "Wheel",
"color" : "blue"
},
{
"@id" : "/mybike/wheels/secondwheel",
"@type" : "Wheel",
"color" : "white"
}
]
}
发送单个请求并获得如下响应是否有效?
GET /mybike HTTP/1.1
Host: wowbike.com
HTTP/1.1 200 OK
Content-Type: application/ld+json
{
"@context" :
{
"Collection": "http://www.w3.org/ns/hydra/core#Collection",
"Bike" : "/contexts/vocab#Bike",
"Wheel" : "/contexts/vocab#Wheel"
},
"@id" : "/mybike",
"@type" : "Bike",
"size" : "L",
"wheels" :
{
"@id" : "/mybike/wheels",
"@type" : "Link",
"member":
[
{
"@id" : "/mybike/wheels/firstwheel",
"@type" : "Wheel",
"color" : "blue"
},
{
"@id" : "/mybike/wheels/secondwheel",
"@type" : "Wheel",
"color" : "white"
}
]
}
}
【问题讨论】:
标签: rest http json-ld hypermedia hydra-core