简体   繁体   English

REST-ish用于多个依赖资源的URI设计

[英]REST-ish URI design for multiple dependent resources

I'm trying to design a REST-ish based Web Service to interface with a farm animal management system I am working on. 我正在尝试设计一个基于REST-ish的Web服务来与我正在研究的农场动物管理系统进行交互。

To explain the problem in detail, I have a collection of animals belonging to a farm. 为了详细解释这个问题,我收集了一个属于农场的动物 Each animal has its own information - such as name, ID number, breed age, etc. Therefore, I would assume a URI such as the following would suit: 每只动物都有自己的信息 - 例如姓名,身份证号码,品种年龄等。因此,我会假设如下所示的URI适合:

/animals               <- retrieve list of animals
/animals/{animal-id}   <- Retrieve only one animal
/animals?breed=sheep   <- Search/query

The problem arises when I try to link in other dependent resources to each animal. 当我尝试将其他依赖资源链接到每只动物时,问题就出现了。 An animal can have a collection of weight information, as well as what I call comments (observations made for a particular animal). 动物可以拥有一系列体重信息,以及我所说的评论 (针对特定动物的观察)。 Each of these are dependent and only exist to that one particular animal, but are themselves a resource I want to access. 这些中的每一个都是依赖的,并且只存在于那个特定的动物中,但它们本身就是我想要访问的资源。

The easiest approach IMO would be to nest the resources within the animal URI: IMO最简单的方法是将资源嵌套在动物URI中:

/animals/{animal-id}/weights
/animals/{animal-id}/comments

However, I see a need to access and query the weights and comments directly, without referencing the animal. 但是,我发现需要直接访问和查询权重注释 ,而无需引用动物。 Examples in usage would be to retrieve the most recent (or all) weight(s) from all animals of a particular breed ...?breed=sheep or even return weights/comments for a selection of individual animal ID's ...?animals={ID1},{ID2},{...} . 使用的例子是从特定品种的所有动物中检索最近(或所有)的重量...?breed=sheep甚至返回重量/评论以选择个体动物ID ...?animals={ID1},{ID2},{...}

Further complications arise when I want to add one comment to multiple animals at once. 当我想一次向多个动物添加一条评论时,会出现更多复杂情况。 (please excuse my representation of a POST and JSON) (请原谅我对POST和JSON的陈述)

POST ....
{
  "comment":"Animal moved to paddock B",
  "animals":[{id1}, {id2}, ...]
}

I understand that the obvious solution to this problem would be to GET and POST (for example) to each animal that I wanted to retrieve/edit. 我知道这个问题的明显解决方案是对我想要检索/编辑的每只动物进行GET和POST(例如)。 I would prefer not to do this though, as eventually I want this service accessed from mobile devices, and therefore decreasing the number of calls seems wise. 我宁愿不这样做,因为最终我希望从移动设备访问这项服务,因此减少通话次数似乎是明智之举。

I believe that the web standards allow CSV in the URI, so something like this could work, 我相信网络标准允许在URI中使用CSV,所以这样的东西可以工作,

/animals/{id1},{id2},{..}/weights

But, I am expecting cases where ten(s) of animals may need to be referenced at once (or queried), and that would lead to a messy and unfriendly URI. 但是,我期待一次(或查询)需要引用十(10)只动物的情况,这将导致一个混乱且不友好的URI。

My current perceived solution is to expose weights and comments as their own resource, which allows me to access and query them directly 我目前认为的解决方案是将权重注释作为自己的资源公开,这允许我直接访问和查询它们

/weights
/weights/{weight-id}
/weights?breed=sheep

and even post directly to the collection 甚至直接发布到收藏品

POST /comments
{
  "comment":"Animal moved to paddock B",
  "animals":[{id1}, {id2}, ...]
}

But what would /animals/{animal-id}/weights return? 但是/animals/{animal-id}/weights返回什么? Is it even needed, or would I just reference a link to the /weights?animal={animal-id} resource itself? 它是否需要,或者我只是引用/weights?animal={animal-id}资源本身的链接? But is it okay to link to a queried resource? 但链接到查询资源是否可以?

Am I making one redundant, or just providing "another" way to access the information? 我是在制作一个多余的,还是只提供“另一种”方式来访问信息?

Is there something that I am doing wrong, am I letting my database influence my service model, or am I just missing the point completely? 有什么东西我做错了,我让我的数据库影响我的服务模型,还是我完全忽略了这一点?

I am quite new to this, and have read quite a few contradicting arguments regarding these issues, and so am quite confused as to what is best for my requirements. 我对此很陌生,并且已经阅读了很多关于这些问题的矛盾论点,因此对于什么对我的要求最好是非常困惑的。

Thanks! 谢谢!

It would be better to define top level resources for your other entities if you want to address them as such ( /weights , /comments etc). 如果您想要为其他实体定义顶级资源( /weights/comments等),那么最好为其他实体定义顶级资源。 You can then POST to those endpoints in a batch fashion. 然后,您可以批量方式POST到这些端点。

POST /comments

{
    "animals" : [
        {"id" : 1},
        {"id" : 2},
        {"id" : 3},
        {"id" : 4},
    ],
    "commentText" : "Sent to Hamburger Hamlet"
}

Note that including long lists of id's in your URL is not good design for several reasons, including that most browsers and HTML proxies have restrictions on URL length (the good rule of thumb is to try and keep URL lengths at 2083 characters in length or less). 请注意,由于多种原因,包括大多数浏览器和HTML代理都限制了网址长度(包括大多数浏览器和HTML代理都限制了网址长度)(包括大多数经验法则都是尝试将网址长度设置为2083个字符或更短)。

I have had similar issues to you, but in the end we were able to remove the complexities you are getting by having specific url namespaces (so to speak) for different user types using the API. 我遇到了类似的问题,但最终我们能够通过使用API​​为不同的用户类型提供特定的URL命名空间(可以这么说)来消除您所获得的复杂性。

For example it might be a farm worker client app that would perform your /weights, /comments actions (POST, PUT, DELETE etc) that you are describing, so you could keep their functionality clean via something like: 例如,它可能是一个农场工人客户端应用程序,它将执行您正在描述的/权重,/注释操作(POST,PUT,DELETE等),因此您可以通过以下方式保持其功能清洁:

/farmworkers/comments
/farmworkers/weights

And then still keep the /animals/{animal-id}/weights URL within some other "namespace". 然后仍然将/animals/{animal-id}/weights URL保留在其他“命名空间”中。

Another thing to consider is embedding resources using something like the HAL format (http://stateless.co/hal_specification.html), which could allow you to embed multiple animal resources within the request etc. Hope this helps. 另一件需要考虑的事情是使用HAL格式(http://stateless.co/hal_specification.html)嵌入资源,这可以允许您在请求中嵌入多个动物资源等。希望这会有所帮助。

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

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