1

我正在 nodejs 中做一个 REST Web 服务器,我正在考虑使用Collection+JSON作为超媒体类型来实现它。

但是我不明白是否可以在集合中表示 1 到 n 关系。因此,例如,如果我有一个集合 Basket,可以使 Basket 中的项目指向其他已定义集合的项目吗?

4

2 回答 2

0

您可以使用collection链接关系,描述为:

指表示当前资源是其成员的集合的资源。

这似乎可以满足您的需求。

于 2014-01-18T11:07:01.983 回答
0

使用传统的电子商务示例,以下是产品的集合 JSON 端点的结构方式。请注意,您的客户端应按 rel-tag 分组。您的 rel 标签列表是您与客户的“合同”。“这个 rel 标签总是意味着那个。”

"collection":
{
  "href": "/products/1234.json",
  "items":
  [
    {
      "name": "name",
      "value": "10 Speed Bike"
    }
    {
      "name": "manufacturer",
      "value": "Schwin"
    }
    {
      "name": "price",
      "value": 150.00
    }
  ],
  "links":
  [
    {
      "href": "/products/1234/attachments/flier.pdf",
      "rel": "attachment",
      "prompt": "Flier from the Manufacturer",
      "render": "link"
    },
    {
      "href": "/products/1234/attachments/specs.pdf",
      "rel": "attachment",
      "prompt": "Spec Sheet from Manufacturer",
      "render": "link"
    },
    {
      "href": "/products/1234/attachments/warranty.pdf",
      "rel": "attachment",
      "prompt": "Limited Lifetime Warranty",
      "render": "link"
    },
    {
      "href": "/store/attachments/returns.pdf",
      "rel": "attachment",
      "prompt": "Our Return Policy",
      "render": "link"
    },
    {
      "href": "/images/1235478324.png",
      "rel": "product-image",
      "prompt": "A Schwin 10-speed bike in red",
      "render": "image"
    },
    {
      "href": "/images/1235478355.png",
      "rel": "product-image",
      "prompt": "A girl riding a 10-speed bike.",
      "render": "image"
    },
    {
      "href": "/images/1235478385.png",
      "rel": "product-image",
      "prompt": "A grandma watching grandkids ride bicycles.",
      "render": "image"
    },
    {
      "href": "/images/1235478545.png",
      "rel": "swatch",
      "prompt": "Radiant Red",
      "render": "image"
    },
    {
      "href": "/images/1235478545.png",
      "rel": "swatch",
      "prompt": "Brilliant Blue",
      "render": "image"
    },
    {
      "href": "/images/1235478588.png",
      "rel": "swatch",
      "prompt": "Stunning Silver",
      "render": "image"
    },
    {
      "href": "/blog/2014/07/26",
      "rel": "content",
      "prompt": "Our store now sells Schwin bicycles!",
      "render": "link"
    },
    {
      "href": "/manufacturers/schwin",
      "rel": "manufacturer",
      "prompt": "Schwin",
      "render": "link"
    },      ]
}

在这个例子中,客户会知道“样本”rel-tag 的存在意味着它应该显示颜色样本部分……每个“rel-tag”对应一个样本。如果有 1 个样本与多个样本,您的客户甚至可能呈现不同的效果。

您想要扩展 collection+JSON 是可以原谅的(我已经广泛扩展了它)。collection+JSON 的冗长语法,我最初认为是一个问题,实际上提供了一种很好的扩展方式,同时保持向后兼容性。

于 2014-08-20T18:39:29.330 回答