2

In the RESTful Web API book, the authors advise to expose a profile and use a content type which acknowledges link relations. JSON-LD extended by Hydra seem to match these requirements, and I want to use them in the design of my new API.

I am currently stuck with a performance issue. Let say that I have an online bike store, and I want to retrieve information about the wheels of a given bike.

With the Hydra specification, it seems to me that I need to send 2 requests to get the details about the wheels. The first request is toward the bike itself:

GET /mybike HTTP/1.1
Host: wowbike.com

The response contains a Hydra::Link to the collection of wheels:

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"
}

Now I can send a second request to the wheels resource to get the details:

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"
                 }
                ]
}

Is it valid to send a single request and get a response such as the one below?

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"
                             }
                            ]
               }

}
4

1 回答 1

6

很高兴看到您考虑使用 JSON-LD 和 Hydra。当然,可以在一个响应中获取所有数据。您不必将集合的类型从更改CollectionLink。此外,您可能需要稍微调整一下上下文。总而言之,您的回复看起来有点像这样:

{
  "@context": [
    "http://www.w3.org/ns/hydra/context.jsonld",
    { "@vocab": "/contexts/vocab#" }
  ],
  "@id": "/mybike",
  "@type": "Bike",
  "size": "L",
  "wheels": {
    "@id"   : "/mybike/wheels",
    "@type" : "Collection",
    "member": [
      {
        "@id"   : "/mybike/wheels/firstwheel",
        "@type" : "Wheel",
        "color" : "blue"
      },
      {
        "@id"   : "/mybike/wheels/secondwheel",
        "@type" : "Wheel",
        "color" : "white"
      }
    ]
  }
}

我在这里导入 Hydra 的上下文,然后覆盖一个默认词汇表,这意味着通过将 Hydra 上下文中尚未定义的所有内容附加到/contexts/vocab#. 因此,Bike例如将扩展为/contexts/vocab#Bike.

顺便提一句。有一个W3C 社区小组正在研究 Hydra,如果您正在使用它,您应该加入它。我们还有一个邮件列表,您的所有问题都将在其中得到解答。

加入小组的说明可以在http://www.hydra-cg.com/#community找到

于 2014-08-14T10:37:26.937 回答