同时使用JSON-LD和Hydra获取子资源集合



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

很高兴看到您考虑使用JSON-LD和Hydra。当然,在单个响应中获得所有数据是可能的。您不必将集合的类型从Collection更改为Link。此外,您可能需要稍微调整一下上下文。总而言之,你的回答应该是这样的:

{
  "@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

找到

相关内容

  • 没有找到相关文章

最新更新