我想知道,有没有办法在 JSON-LD 中使用 HAL 概念?
我有当前的 jsonld 文档:
{
"@context": {
"hal": "http://stateless.co/hal#",
"schema": "http://schema.org",
"_links": {
"@id": "hal:link",
"@container": "@index"
}
},
"@type": ["schema:Person", "hal:Resource"],
"name": "Jon Snow",
"_links": {
"self": {
"href": "/users/123"
}
}
}
但我不确定如何定义
href
有一个 @type
的 @id
, 等等...有没有办法定义基于 RDF(S) 的 HAL 词汇并以某种方式将它导入到我的 jsonld 文档的 @context 中,或者我应该做其他事情吗?
(我试图用各种属性来描述超链接,比如链接关系、HTTP 方法、接受的媒体类型、语言、IRI 模板、输入字段等......所以
@id
类型对我来说不足以描述链接。)
最佳答案
正如 Tomasz 已经建议的那样,你真的应该考虑使用 Hydra因为它或多或少是你想要的。您在问题中包含的示例使用 Hydra 和 JSON-LD 看起来有点像这样:
{
"@context": {
"schema": "http://schema.org",
"ex": "http://example.com/myvocab#"
},
"@id": "/users/123",
"@type": [ "schema:Person", "hydra:Resource" ],
"name": "Jon Snow",
"ex:link": { "@id": "/another-resource" }
}
由于不需要“自我”链接(
@id
已经明确指定),我添加了另一个链接,ex:link
.因此它的链接关系是http://example.com/myvocab#link
它的“href”是 /another-resource
.如果您需要更详细地描述该链接/属性,您可以通过创建一个文档来实现,该文档以与其他内容完全相同的方式定义它(正如 Tomasz 也已经解释过的):{
"@context": {
"ex": "http://example.com/myvocab#",
"hydra": "http://www.w3.org/ns/hydra#"
},
"@id": "ex:link",
"@type": "hydra:Link",
"hydra:title": "My new link relation",
"hydra:supportedOperation": [
{
"@type": "hydra:Operation",
"hydra:method": "POST",
"hydra:expects": ....
}
]
}
关于你的评论
Btw. I am more or less familiar with the Hydra vocab, but I don't like the idea to map the resources to real classes and objects on a server side language and automatically transform the operation parameters into those objects. Maybe it is possible to use the Hydra vocab in another way, but I don't have the time to experiment with that.
Hydra实际上只是一个词汇。由您决定如何使用它。我想你在谈论 HydraBundle以上。这只是使用它的一种方式。这只是一个概念证明,表明它很容易实现。所以请不要对此感到困惑。
我想邀请您到join the Hydra W3C Community Group .然后我们可以在我们的邮件列表中更详细地讨论这个问题。
免责声明:我是 Hydra 的创建者和 Hydra W3C 社区组的主席。
关于rest - 将 HAL 词汇与 JSON-LD 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24421234/