如何模型继承一个RESTful API? [英] How to model a RESTful API with inheritance?

查看:130
本文介绍了如何模型继承一个RESTful API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象层次,我需要通过一个RESTful API来揭露和我不知道我的网址应如何构建,他们应该返回什么。我找不到任何的最佳做法。

I have an object hierarchy I need to expose through a RESTful API and I'm not sure how my URLs should be structured and what they should return. I could not find any best practices.

让我们说我有狗和猫从动物身上继承。我需要狗和猫CRUD操作;我也希望能够做到对一般动物的操作。

Let's say I have Dogs and Cats inheriting from Animals. I need CRUD operations on dogs and cats; I also want to be able to do operations on animals in general.

我的第一个想法是做这样的事情:

My first idea was to do something like this:

GET /animals        # get all animals
POST /animals       # create a dog or cat
GET /animals/123    # get animal 123

的事情是,在/动物集合现在是不一致,因为它可以返回,并采取不具有完全相同的结构(狗和猫)对象。难道认为基于REST的有一个集合返回对具有不同属性的对象?

The thing is that the /animals collection is now "inconsistent", as it can return and take objects that do not have exactly the same structure (dogs and cats). Is it considered "RESTful" to have a collection returning objects that have differing attributes?

另一种解决方案是创建一个URL对于每个具体的类型,如:

Another solution would be to create an URL for each concrete type, like this:

GET /dogs       # get all dogs
POST /dogs      # create a dog
GET /dogs/123   # get dog 123

GET /cats       # get all cats
POST /cats      # create a cat
GET /cats/123   # get cat 123

但是,现在的狗和猫之间的关系被丢失了。如果希望检索所有动物中,狗和猫都资源必须查询。网址数量也将增加,每个新的动物类型。

But now the relationship between dogs and cats is lost. If one wishes to retrieve all animals, both the dog and cat resources must be queried. The number of URLs will also increase with each new animal subtype.

另一项建议是通过添加这增加第二个解决方案:

Another suggestion was to augment the second solution by adding this:

GET /animals    # get common attributes of all animals

在这种情况下,动物返回将只包含共同所有动物的属性,滴加犬特异性和猫特定属性。这使得检索所有的动物,虽然使用较少的信息。每个返回的对象可以包含一个链接到详细,具体的版本。

In this case, the animals returned would only contain attributes common to all animals, dropping dog-specific and cat-specific attributes. This allows to retrieve all animals, although with fewer details. Each returned object could contain a link to the detailed, concrete version.

任何意见或建议?

推荐答案

我建议:


  • 使用每个资源只有一个URI

  • 在属性级别的动物之间的区别仅仅

多个URI设置为相同的资源从来都不是一个好主意,因为它可能会导致混乱和意外的副作用。鉴于此,你的单身URI应基于像普通的方案 /动物

Setting up multiple URIs to the same resource is never a good idea because it can cause confusion and unexpected side effects. Given that, your single URI should be based on a generic scheme like /animals.

与狗和猫的整个集合处理在基地级别的下一个挑战已经凭借 /动物 URI的方式来解决。

The next challenge of dealing with the entire collection of dogs and cats at the "base" level is already solved by virtue of the /animals URI approach.

专门的类型,如狗和猫打交道就可以轻松解决使用中的查询参数和标识组合的最后一个挑战你的媒体类型中的属性。例如:

The final challenge of dealing with specialized types like dogs and cats can be easily solved using a combination of query parameters and identification attributes within your media type. For example:

GET /动物接受:应用/ vnd.vet-services.animals + JSON

{
   "animals":[
      {
         "link":"/animals/3424",
         "type":"dog",
         "name":"Rex"
      },
      {
         "link":"/animals/7829",
         "type":"cat",
         "name":"Mittens"
      }
   ]
}


  • GET /动物 - 得到所有狗和猫,会同时返回雷克斯和手套

  • GET /动物类型=狗 - ?得到所有的狗,只会返回雷克斯

  • GET /动物类型=猫 - ?得到所有的猫,那只手套

    • GET /animals - gets all dogs and cats, would return both Rex and Mittens
    • GET /animals?type=dog - gets all dogs, would only return Rex
    • GET /animals?type=cat - gets all cats, would only Mittens
    • 然后创建或修改的动物时,这将是对呼叫者责任指定动物所涉及的类型:

      Then when creating or modifying animals, it would be incumbent on the caller to specify the type of animal involved:

      介质类型:应用程序/ vnd.vet-services.animal + JSON

      {
         "type":"dog",
         "name":"Fido"
      }
      

      以上的有效载荷可以用 POST PUT发送请求。

      以上方案让你的基本相似的特性OO继承通过REST,并在没有重大手术或您的URI方案的任何变化进一步增加专业(即更多的动物种类)的能力。

      The above scheme gets you the basic similar characteristics as OO inheritance through REST, and with the ability to add further specializations (i.e. more animal types) without major surgery or any changes to your URI scheme.

      这篇关于如何模型继承一个RESTful API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆