简体   繁体   English

REST API资源依赖关系

[英]REST api resource dependencies

I have this data management panel of IP addresses, which belong to organization and have users responsible for it. 我有这个IP地址的数据管理面板,该面板属于组织并由用户负责。

Now I have the route /api/ip and /api/ip/{id} to get all or specific IP. 现在,我有了路由/api/ip/api/ip/{id}来获取所有或特定的IP。 The format of one resource is: 一种资源的格式为:

{
    "ip": "200.0.0.0",
    "mask": 32,
    "broadcast": "200.0.0.1"
}

Now when I choose the IP, I want to show IP information, also the organization information it belongs to and the users, that are responsible for it, information in one page. 现在,当我选择IP时,我想在一个页面中显示IP信息,以及它所属的组织信息以及负责该信息的用户。

Is it good idea to return the following data format, while requiring /api/ip/{id} : 在需要/api/ip/{id}同时返回以下数据格式是个好主意:

{
    "ip": "200.0.0.0",
    "mask": 32,
    "broadcast": "200.0.0.1",
    "organization": { /* organization data */ },
    "users": { /* users information */ }
}

This way I get all the information I need in one request, but is it still RESTful API? 这样,我可以在一个请求中获得所需的所有信息,但是它仍然是RESTful API吗?

Or should I make 2 more api routes like /api/ip/{id}/organization and /api/ip/{id}/users and get all the data I need in 3 separate requests? 还是应该再增加2条api路由(例如/api/ip/{id}/organization/api/ip/{id}/users并在3个单独的请求中获取所需的所有数据?

If not, what would be the appropriate way of doing this? 如果没有,那么合适的方法是什么?

I would do the last one, using Hateoas, which allows you to link between the resources. 我将使用Hateoas做最后一个,它允许您在资源之间进行链接。 There is a really great bundle for that called the BazingaHateoasBundle . 有一个非常好的捆绑包,叫做BazingaHateoasBundle The result will then be something like: 结果将是这样的:

/api/ip/127.0.0.1

{
    "ip": "200.0.0.0",
    "mask": 32,
    "broadcast": "200.0.0.1",
    "_links": {
        "organization": "/api/ip/127.0.0.1/organization",
        "users": "/api/ip/127.0.0.1/users"
    }
}

It is perfectly okay to have nested resources. 嵌套资源是完全可以的。 You can expand them the way you showed, or you can collapse them by adding links (with the proper link relation or RDF metadata). 您可以按照显示的方式展开它们,也可以通过添加链接(具有正确的链接关系或RDF元数据)来折叠它们。 I suggest you to use a standard or at least documented hypermedia type, eg JSON-LD + Hydra, or HAL+JSON. 我建议您使用标准或至少已记录的超媒体类型,例如JSON-LD + Hydra或HAL + JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM