我有一个接受JSON参数的web服务,并有特定的方法url,例如:

http://IP:PORT/API/getAllData?p={JSON}

这肯定不是REST,因为它不是无状态的。它会考虑cookie,并有自己的会话。

是RPC吗?RPC和REST之间的区别是什么?


当前回答

在HTTP上,它们都只是HttpRequest对象,它们都期望返回一个HttpResponse对象。我认为人们可以继续用这种描述来编码,而不用担心其他的事情。

其他回答

这里有很多很好的答案。我仍然建议你参考谷歌的这个博客,因为它在讨论RPC和REST之间的区别方面做得非常好,并抓住了一些我在这里的任何答案中都没有读到的东西。

我想引用同一链接中的一段话,这段话让我印象深刻:

REST itself is a description of the design principles that underpin HTTP and the world-wide web. But because HTTP is the only commercially important REST API, we can mostly avoid discussing REST and just focus on HTTP. This substitution is useful because there is a lot of confusion and variability in what people think REST means in the context of APIs, but there is much greater clarity and agreement on what HTTP itself is. The HTTP model is the perfect inverse of the RPC model—in the RPC model, the addressable units are procedures, and the entities of the problem domain are hidden behind the procedures. In the HTTP model, the addressable units are the entities themselves and the behaviors of the system are hidden behind the entities as side-effects of creating, updating, or deleting them.

因此,我认为:

我的实体是否持有/拥有数据?然后RPC:这是我的一些数据的副本,操作我发送给你的数据副本,并返回给我你的结果的副本。

被调用的实体是否持有/拥有数据?然后REST:要么(1)向我展示你的一些数据的副本,要么(2)操纵你的一些数据。

归根结底,这是关于行为的哪一方拥有/持有数据。是的,您可以使用REST语言与基于RPC的系统进行通信,但是在这样做的时候仍然会执行RPC活动。

示例1:我有一个对象,它通过DAO与关系数据库存储(或任何其他类型的数据存储)通信。对于我的对象和数据访问对象(可以作为API存在)之间的交互,使用REST样式是有意义的。我的实体并不拥有/持有数据,而是关系数据库(或非关系数据存储)拥有。

Example 2: I need to do a lot of complex math. I don't want to load a bunch of math methods into my object, I just want to pass some values to something else that can do all kinds of math, and get a result. Then RPC style makes sense, because the math object/entity will expose to my object a whole bunch of operations. Note that these methods might all be exposed as individual APIs and I might call any of them with GET. I can even claim this is RESTful because I am calling via HTTP GET but really under the covers it is RPC. My entity owns/holds the data, the remote entity is just performing manipulations on the copies of the data that I sent to it.

在HTTP上,它们都只是HttpRequest对象,它们都期望返回一个HttpResponse对象。我认为人们可以继续用这种描述来编码,而不用担心其他的事情。

您不能仅仅通过查看您发布的内容来明确区分REST或RPC。

REST的一个约束是它必须是无状态的。如果你有一个会话,那么你就有一个状态,所以你不能把你的服务称为RESTful的。

事实上,您在URL中有一个操作(即getAllData)是对RPC的指示。在REST中,您交换表示,您执行的操作由HTTP谓词指定。同样,在REST中,内容协商不是使用?p={JSON}参数执行的。

不知道您的服务是否是RPC,但它不是RESTful的。你可以在网上了解它们的区别,这里有一篇文章可以让你开始:揭穿RPC和REST的神话。您更了解服务内部的内容,因此将其函数与RPC进行比较,并得出自己的结论。

正如其他人所说,一个关键的区别是REST url以名称为中心,而RPC url以动词为中心。我只是想包括这个清晰的例子表来证明:

---------------------------+-------------------------------------+--------------------------
 Operation                 | RPC (operation)                     | REST (resource)
---------------------------+-------------------------------------+--------------------------
 Signup                    | POST /signup                        | POST /persons           
---------------------------+-------------------------------------+--------------------------
 Resign                    | POST /resign                        | DELETE /persons/1234    
---------------------------+-------------------------------------+--------------------------
 Read person               | GET /readPerson?personid=1234       | GET /persons/1234       
---------------------------+-------------------------------------+--------------------------
 Read person's items list  | GET /readUsersItemsList?userid=1234 | GET /persons/1234/items 
---------------------------+-------------------------------------+--------------------------
 Add item to person's list | POST /addItemToUsersItemsList       | POST /persons/1234/items
---------------------------+-------------------------------------+--------------------------
 Update item               | POST /modifyItem                    | PUT /items/456          
---------------------------+-------------------------------------+--------------------------
 Delete item               | POST /removeItem?itemId=456         | DELETE /items/456       
---------------------------+-------------------------------------+--------------------------

笔记

As the table shows, REST tends to use URL path parameters to identify specific resources (e.g. GET /persons/1234), whereas RPC tends to use query parameters for function inputs (e.g. GET /readPerson?personid=1234). Not shown in the table is how a REST API would handle filtering, which would typically involve query parameters (e.g. GET /persons?height=tall). Also not shown is how with either system, when you do create/update operations, additional data is probably passed in via the message body (e.g. when you do POST /signup or POST /persons, you include data describing the new person). Of course, none of this is set in stone, but it gives you an idea of what you are likely to encounter and how you might want to organize your own API for consistency. For further discussion of REST URL design, see this question.