RESTful 多对多可能吗? [英] RESTful Many-to-Many possible?

查看:38
本文介绍了RESTful 多对多可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,目前我有一个应用程序,当用户点击保存"时,它会遍历所有表单元素并创建一个管理以下内容的质量对象:

Hello, Currently I have an application which when the user hits "save" it iterates over all of the form elements and creates one mass object which manages a:

  var = params = [{ 
   attributes1: form1.getValues(),
   attributes2: form2.getValues(),  
.. ..
}];

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

I then send this mass object via a RPC POST to my "Entity" model service. This entity which I wish to persist data for is quite complex. All in all, the data is spread accross about 30 tables. To help explain my actual question, the "entity" is a building (as in a physical property/house/apartment).

我想要的是能够将我的混乱变成用于保存属性的 RESTful API.我遇到的问题是,为跨单个表的单个模型保存详细信息是可以的.当模型具有时,我如何构造用于传输的数据对象

What I would like, is to be able to turn my mess into a RESTful API for saving properties. The problem I have is that, saving details for a single model that spans a single table is fine. How do I structure my data object for transport when the model has

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

例如:

这是我可能拥有的财产和样本数据的水化版本

Here is a WATERED down version of what I might have on a property and the sample data

propertyId: 1,
locationId: 231234,
propertyName: "Brentwood",
kitchenFeatures: [
             { featureId: 1, details: "Induction hob"},
             { featureId:23, details: "900W microwave"}
],
propertyThemes: [ 12,32,54,65 ]

这实际上还有很多……但您可以了解一般要点.kitchenFeatures 将是多对多的一个例子,其中我有一个 featuresTable,它具有这样的所有功能:

This actually goes on a lot more.. but you can get the general gist. kitchenFeatures would be an example of a many-to-many, where I have a featuresTable which has all of the features like so:

`featureId`, `feature`
1             "Oven Hob"  
23            "Microwave"

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

and propertyThemes would be an example of another many-to-many.

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

How am I expected to form my "object" to my RESTful service? Is this even possible?

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

ie. If I want to save this property I would send it to:

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

推荐答案

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

The approach I would use here is hypermedia and links:

/property
/property/{id}
/property/{id}/features/{id}

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

Depending on your domain you might even get away with:

/property/{id}/features/{name}

/property/{id}/features/byname/{name}

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

Thus you can do REST operations and serve JSON or XHTML hypermedia.

物业详情:

Request: GET /property/1
Response:
{
  ..
  "name": "Brentwood",
  "features": "/property/1/features"
  ..
}

布伦特伍德的特点:

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",
  ..
}

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

To add a relation you can do something like this:

POST /property/1/features
{
  ..
  "Name": "Oven Hob"
  ..
}

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

If you know what the relation will be you use a PUT:

PUT /property/1/features/23
{
  ..
  "Name": "Oven Hob"
  ..
}

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

You can serve multiple media types:

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

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

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

For the response in xhtml the response can use named links like this:

..
 <a href="http://host/property/1/features/1" rel="prev">Kitchen</a>
..

您还可以使用 REST 的其他方面,例如我上面没有包括的响应代码.

There are other aspects of REST that you can use such as response code which I did not include above.

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

Thus, to model relations you make use of links which can be in itself a resource that can be operated on with GET, PUT, POST and DELETE or even custom verbs such as ASSOCIATE or LINK. But the first four are the ones that people are used to. Remember PUT is idempotent but not POST. See PUT vs POST in REST

您可以将链接分组到 JSON 数组中,以便为您的超媒体提供结构.

You can group your links into JSON arrays to give structure to your hypermedia.

这篇关于RESTful 多对多可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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