RESTful多对多可能吗?

Lay*_*yke 11 rest

如何为REST帖子表示复杂资源?

您好,目前我有一个应用程序,当用户点击"保存"时,它会遍历所有表单元素并创建一个管理:

  var = params = [{ 
   attributes1: form1.getValues(),
   attributes2: form2.getValues(),  
.. ..
}];
Run Code Online (Sandbox Code Playgroud)

然后我通过RPC POST将此批量对象发送到我的"实体"模型服务.我希望保留数据的这个实体非常复杂.总而言之,数据分布在大约30个表格中.为了帮助解释我的实际问题,"实体"是一个建筑物(如物理/房屋/公寓).

我想要的是能够将我的混乱变成RESTful API以保存属性.我遇到的问题是,保存跨越单个表的单个模型的详细信息很好.如何在模型具有的情况下构建数据对象以进行传输

  • 多对多的关系
  • 一对多关系
  • 一对一的关系

例如:

这是我在房产和样本数据上可能拥有的WATERED版本

propertyId: 1,
locationId: 231234,
propertyName: "Brentwood",
kitchenFeatures: [
             { featureId: 1, details: "Induction hob"},
             { featureId:23, details: "900W microwave"}
],
propertyThemes: [ 12,32,54,65 ]
Run Code Online (Sandbox Code Playgroud)

这实际上还有很多......但你可以得到一般的要点.kitchenFeatures将是一个多对多的例子,我有一个featuresTable具有如下所有的功能:

`featureId`, `feature`
1             "Oven Hob"  
23            "Microwave"
Run Code Online (Sandbox Code Playgroud)

和propertyThemes将是另一个多对多的例子.

我希望如何形成我的RESTful服务的"对象"?这甚至可能吗?

即.如果我想保存这个属性,我会发送给:

http://example.com/api/property/1

Der*_*bee 10

我在这里使用的方法是超媒体和链接:

/property
/property/{id}
/property/{id}/features/{id}
Run Code Online (Sandbox Code Playgroud)

根据您的域名,您甚至可以逃脱:

/property/{id}/features/{name}
Run Code Online (Sandbox Code Playgroud)

要么

/property/{id}/features/byname/{name}
Run Code Online (Sandbox Code Playgroud)

因此,您可以执行REST 操作并提供JSON或XHTML 超媒体.

物业详情:

Request: GET /property/1
Response:
{
  ..
  "name": "Brentwood",
  "features": "/property/1/features"
  ..
}
Run Code Online (Sandbox Code Playgroud)

布伦特伍德的特点:

GET /property/1/features
{
  ..
  "Kitchen": "/property/1/features/1",
  "Dog Room": "/property/1/features/dog%20room",
  ..
}

GET /property/1/features/1
{
  ..
  "Induction hob": "/property/1/features/1/1",
  "900W microwave": "/property/1/features/1/23",
  "nav-next" : "/property/1/features/dog%20room",
  ..
}
Run Code Online (Sandbox Code Playgroud)

要添加关系,您可以执行以下操作:

POST /property/1/features
{
  ..
  "Name": "Oven Hob"
  ..
}
Run Code Online (Sandbox Code Playgroud)

如果您知道关系是什么,请使用PUT:

PUT /property/1/features/23
{
  ..
  "Name": "Oven Hob"
  ..
}
Run Code Online (Sandbox Code Playgroud)

您可以提供多种媒体类型:

GET http://host/property/1/features/dog%20room.json

GET http://host/property/1/features/dog%20room.xhtml
Run Code Online (Sandbox Code Playgroud)

对于xhtml中的响应,响应可以使用如下命名链接:

..
 <a href="http://host/property/1/features/1" rel="prev">Kitchen</a>
..
Run Code Online (Sandbox Code Playgroud)

您可以使用REST的其他方面,例如上面未包含的响应代码.

因此,要建模关系,您可以使用链接,这些链接本身可以是可以使用GET,PUT,POST和DELETE操作的资源,甚至可以是自定义动词,例如ASSOCIATE或LINK.但前四个是人们习惯的.记住PUT是幂等的但不是POST.请参阅REST中的PUT与POST

编辑:您可以将链接分组为JSON数组,以便为​​超媒体提供结构.