RESTful兼容设计 [英] RESTful compliant design

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

问题描述

让我们考虑一下我需要开发一个REST银行应用程序,
允许创建/销毁银行账户以及账户操作后的
:withdraw / credit / getBalance。

Let's consider I need to develop a REST bank application that allows the creation/destruction of bank accounts as well as the following operations on an account: withdraw/credit/getBalance.


  • 创建账户

PUT / Bank / john

PUT /Bank/john

这里我使用PUT而不是POST,因为这个操作是
idempotent,因为客户端正在提供URL

Here I use PUT instead of POST because this operation is idempotent and because the client is giving the URL


  • 销毁帐户

DELETE / Bank / john

DELETE /Bank/john


  • GetBalance

GET / Bank / john

GET /Bank/john


  • 从账户提取资金

POST / Bank / john

POST /Bank/john

action = withdraw& value = 10

action=withdraw&value=10


  • 向账户存入信用卡

POST / Bank / john

POST /Bank/john

action = credit& value = 10

action=credit&value=10

在这里,我使用POST,因为提款/信贷显然不是t idempotent

Here, I used POST because withdrawal/credit are clearly not idempotent

它是一种设计这些操作的RESTful兼容方式吗?

is it a RESTful compliant way of designing these operations ?

我觉得我是通过放置动词来写出类似RPC的东西(撤回|在
里面的动作参数..我经常读到REST不应该模仿类似RPC的样式...

I have the feeling that I am writing something that is RPC-like by putting the verbs (withdraw | credit) inside the action parameter .. and I often read that REST should not mimic the RPC-like style ...

推荐答案

在处理REST时,通常从资源方面考虑是有帮助的。在这种情况下,您的资源不仅仅是您的银行帐户,而且还是该银行帐户的交易。

When dealing with REST, it generally helps to start by thinking in terms of resources. In this case your resource is not just your "bank account" but it is a transaction of that bank account.

存款

POST /Bank/Account/John/Transaction

currency=USD&amount=10

Withdraw

POST /Bank/Account/John/Transaction

currency=USD&amount=-10

您的回复应包含此新创建的交易的位置标头。

You response should include a Location header to this newly created transaction.

您正在创建一个交易。这样做的好处是您可以将该交易作为资源引用。

You are creating a transaction. The advantage of this is that you can then reference that transaction as a resource.

GET /Bank/Account/John/Transaction/12345

这可能会返回该确切交易的记录(例如,您的用户通常需要记录借记和他们的帐户上的积分)。

This could return a record of that exact transaction (e.g. your users generally want a record of debits and credits on their account).

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

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