进行更新许多相关实体的 RESTful API 调用
我们有一个看起来像这样的模型
Login <- Email Addresses <- Person -> Teen
和一个存储过程,它从 teen 获取一些属性,从 person 获取一些属性,从 Login 获取一些属性,并创建一个新的 teen,返回一个 person 实体。
从经典的 RPC 角度来看,这很简单......只需公开一个方法 InsertTeen
并让它调用存储过程即可。
我一直试图理解 RESTful 的想法,即将 URL 作为我的资源(名词),唯一的操作是 HTTP 操作(动词)。显然,像 /api/InsertTeen
这样的 URL 根本不是 RESTful。
但在这里我不处理任何特定的资源。
我在这里唯一能做的就是公开像 insertTeenRequest
这样的资源。
对于如何做到这一点还有其他想法吗?我是不是太“狂热”了?
We have a model that looks like this
Login <- Email Addresses <- Person -> Teen
And a stored procedure which takes some properties from teen, some from person, and some from Login, and creates a new teen, returning a person entity.
Looking from a classic RPC perspective, this is easy...just expose a method InsertTeen
and have it call the stored procedure.
I've been trying to wrap my head around the RESTful idea of having URLs as my resources (nouns), and the only actions being HTTP actions (verbs). Obviously, a URL like /api/InsertTeen
is not RESTful at all.
But here I'm not dealing with any particular resource.
The only thing I can thing of here would be to expose a resource like insertTeenRequest
.
Are there any other ideas of how to do this? Am I being too much of a "zealot"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要真正实现 RESTful,在这种情况下您应该对 API 使用多个请求。例如,首先使用 POST 到 /api/teens/ 创建
Teen
,然后使用 POST 到 /api/persons/ 创建Person
等等。If you want to be really RESTful, you should use several requests to your API in this case. For example first you create
Teen
with POST to /api/teens/, then createPerson
with POST to /api/persons/ and so on.我自己对 REST 很陌生,但我的想法是,在这里您可以使用“POST”,请求正文包含创建“Teen”所需的数据,无论您使用什么格式,通常是 JSON 或 XML。在这里,我不确定您是否将青少年视为具有附加属性的人,或者将青少年建模为实体本身:
或者
关于 URI,我认为这些段应该是名词而不是动词,因为 URI 应该解决资源,因此
/api/teens
而不是/api/InsertTeen
。使用 HTTP GET 的
/api/teens
将返回所有青少年的列表,使用 HTTP POST 的/api/teens
将插入一个新的青少年。为了完成 CRUD 操作,使用 HTTP GET 的/api/teens/{id}
将使用 HTTP PUT 返回特定的 Teen,/api/teens/{id}
将使用请求正文中传递的值更新 Teen,并且使用 HTTP DELETE 调用的/api/teens/{id}
将删除指定的 Teen。编辑
再读一遍你的问题,我可能误解了。如果您不将“青少年”视为资源,而仅将“人”视为资源,那么我会考虑使用 HTTP POST 的
/api/people
,并取决于在正文中传递的值请求,采取任何适当的措施来存储该“人”。因此,如果请求包含“青少年”值,请调用创建“青少年”并返回“个人”的存储过程。华泰
Pretty new to REST myself, but my thinking is that here you would use a "POST" with the body of the request containing the data needed to create a 'Teen', in whatever format you are using, usually JSON or XML. Here, I'm not sure whether you treat Teens as Persons with additional properties, or a Teen is modeled as an entity itself:
or
Regarding the URI, I believe the segments should be nouns rather than verbs since the URI is supposed to address a resource, so
/api/teens
rather than/api/InsertTeen
./api/teens
with an HTTP GET would return a list of all Teens, and/api/teens
with an HTTP POST would insert a new Teen. To round out the CRUD operations,/api/teens/{id}
using HTTP GET would return a specific Teen,/api/teens/{id}
with an HTTP PUT would update a Teen using the values passed in the request body, and/api/teens/{id}
called with HTTP DELETE would delete the specified Teen.Edit
Read over your question again, and I may have misunderstood. If you aren't treating 'teens' as a resource, but only 'people', then I would consider
/api/people
with an HTTP POST, and depending on the values passed in the body of the request, do whatever is appropriate to store that 'person'. So, if the request contained 'teen' values, call your stored procedure that creates a 'Teen' and returns a 'Person'.HTH