如何处理JSON中的HATEOAS链接和引用?

时间:2022-08-23 08:32:03

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.

我正在设计一个REST api,并尽可能地保持RESTful。我想将HATEOAS合并到json响应中。

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

将url添加到相关资源非常简单,但是对这些链接使用的结构进行了一些讨论。

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

我发现很多文章使用了从ATOM提要借来的结构:

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

    为什么要使用数组作为容器?根据我认识的一位javascript开发人员的说法,使用链接作为对象的属性将更容易访问链接。例如:

    "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).

    是否有一个公共的json结构(除了修改atom之外)来描述资源属性中的引用?(例如消息的发送方)。

    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:

    引用应该再次解析为url,但是包含简单的id是否也不好呢?就像:

    "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).

我认为的方式是,尽管HATEOAS使它所以客户机不需要很多知识使用API,我有点不愿意删除可能使用这些知识(如访问配置文件图片通过构建客户端没有查找用户的联系)。

3 个解决方案

#1


25  

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

我在API-Craft谷歌组重新启动了这个话题,得到了一些很好的回答。

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
  • 为同一个链接编写多个关系而不编写链接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.

我想我还是跟哈尔一起去吧,因为这是最干净的解决方案,其余的看起来都有点……奇怪的json。

#2


4  

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

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

#3


0  

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

我认为它可以提供基于http方法的多个链接。

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

#1


25  

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

我在API-Craft谷歌组重新启动了这个话题,得到了一些很好的回答。

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
  • 为同一个链接编写多个关系而不编写链接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.

我想我还是跟哈尔一起去吧,因为这是最干净的解决方案,其余的看起来都有点……奇怪的json。

#2


4  

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

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

#3


0  

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

我认为它可以提供基于http方法的多个链接。

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