我应该如何处理JSON中的HATEOAS链接和引用?[英] How should I handle HATEOAS links and references in JSON?

本文是小编为大家收集整理的关于我应该如何处理JSON中的HATEOAS链接和引用?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在设计一个REST API,并且要保持静止.我想结合 hateoas

将URL添加到相关资源很容易,但是关于这些链接的结构进行了一些讨论.

我发现很多文章都使用了从 Atom ">原子 feeds feeds feeds:

"links": [ 
    {"rel": "self", "href":"http://example.org/entity/1"},
    {"rel": "friends", "href":"http://example.org/entity/1/friends"}, ... 
]

这提出了一些问题:

  • 为什么根据我知道的JavaScript开发人员使用数组作为容器?例如:

    "self":    { "href":"http://example.org/entity/1" }, /* (facebook uses this) */  
    "friends": { "href":"http://example.org/entity/1/friends", "type": "..."}
    
  • 是否有常见的JSON结构(再次适应原子)以描述资源属性中的参考?(例如,消息的发送者).

    参考可能再次作为URL解决,但是包括简单的ID也是不好的吗?有点像:

    "sender": { 
        "id": 12345,
        "href": "resource-uri"
    }
    

我的思考方式是,虽然Hateoas使客户不需要很多知识来使用API​​,但我不愿意消除使用该知识的可能性(例如,通过访问配置文件图片构建链接客户端而不先查找用户).

推荐答案

我在API-Craft Google组上重新启动了此主题,并得到了一些很好的答复.

数组设计的主要优点是:

  • 相同关系的多个链接
  • 相同链接的多个关系而不编写链接again
  • 订购链接的能力

原因图具有更好的可访问性.

就结构而言,有很多可能性:

我想我会选择HAL,因为它是最干净的解决方案,其余的都看起来有点... JSON奇怪.

其他推荐答案

就结构而言,您可以尝试查看hal( http://stateless.co/hal_specification.html )或JSON-LD:( http://json-ld.org/ )

其他推荐答案

我认为它可以基于HTTP方法提供多个链接.

例如.

"links": [ 
    {"rel": "sender", "method":"post", "href":"http://example.org/entity/1"},
    {"rel": "sender", "method":"put", "href":"http://example.org/entity/1"}, ... 
]

也许您可以将其适应您的想法

"sender": { 
     "href":"http://example.org/entity/1",
     "methods": ["put","post"]
}

本文地址:https://www.itbaoku.cn/post/627360.html

问题描述

I'm in the process of designing a REST api and to be as RESTful as it gets. I want to incorporate HATEOAS into the json responses.

Adding URLs to related resources is easy enough, but there was some discussion over the structure to use for those links.

A LOT of articles I found use a structure borrowed from ATOM feeds:

"links": [ 
    {"rel": "self", "href":"http://example.org/entity/1"},
    {"rel": "friends", "href":"http://example.org/entity/1/friends"}, ... 
]

This raised some questions:

  • Why use an array as a container? According to a javascript developer I know, access to the links would be easier with the links as properties of an object. For example:

    "self":    { "href":"http://example.org/entity/1" }, /* (facebook uses this) */  
    "friends": { "href":"http://example.org/entity/1/friends", "type": "..."}
    
  • Is there a common json structure (beside adapting atom again) to describe references in resource properties? (for example the sender of a message).

    The reference should probably be resolved as a url again, but would it be bad to include the simple id as well? kind of like:

    "sender": { 
        "id": 12345,
        "href": "resource-uri"
    }
    

My way of thought is that while HATEOAS makes it so a client doesn't need a lot of knowledge to use an API, I'm kind of reluctant to remove the possibility to USE that knowledge (like accessing the profile picture by building the link client-side without looking up the user first).

推荐答案

I restarted this topic on the API-Craft google group and got some great responses.

The main advantages of the Array design are:

  • multiple links for the same relationship
  • multiple relationships for the same link without writing the link aggain
  • the ability to order the links

The map of cause has better accessibility.

As far as structure goes there are a lot of possibilities:

I guess i will go with HAL as it is the cleanest solution, the rest all look kind of... strange for json.

其他推荐答案

As far as structure goes, you could try looking at HAL (http://stateless.co/hal_specification.html) or JSON-LD: (http://json-ld.org/)

其他推荐答案

I think its so that you can offer multiple links based on the http method.

e.g.

"links": [ 
    {"rel": "sender", "method":"post", "href":"http://example.org/entity/1"},
    {"rel": "sender", "method":"put", "href":"http://example.org/entity/1"}, ... 
]

maybe you could adapt that to your idea

"sender": { 
     "href":"http://example.org/entity/1",
     "methods": ["put","post"]
}