我试图在REST和JSON-RPC之间做出选择,为web应用程序开发API。它们是如何比较的?

更新2015:我发现REST在Web/HTTP上的API上更容易开发和使用,因为API可以利用客户端和服务器都理解的现有和成熟的HTTP协议。例如,API不需要任何额外的工作或设置就可以使用响应代码、报头、查询、帖子正文、缓存和许多其他特性。


当前回答

根据Richardson成熟度模型,问题不是REST vs. RPC,而是多少REST?

从这个角度来看,REST标准的遵从性可以分为4个级别。

0级:从动作和参数的角度考虑。正如本文所解释的,这在本质上等同于JSON-RPC(本文对XML-RPC进行了解释,但两者的参数相同)。 第一级:从资源的角度考虑。与资源相关的所有内容都属于同一个URL 第2级:使用HTTP动词 第三级:HATEOAS

According to the creator of REST standard, only level 3 services can be called RESTful. However, this is a metric of compliance, not quality. If you just want to call a remote function that does a calculation, it probably makes no sense to have relevant hypermedia links in the response, neither differentiation of behavior based on the HTTP verb used. So, a such call inherently tends to be more RPC-like. However, lower compliance level does not necessarily mean statefulness, or higher coupling. Probably, instead of thinking REST vs. RPC, you should use as much REST as possible, but no more. Do not twist your application just to fit with the RESTful compliance standards.

其他回答

在我看来,关键在于行动vs资源导向。REST是面向资源的,非常适合CRUD操作,并且由于其已知的语义为第一个用户提供了一些可预测性,但是当从方法或过程实现时,将迫使您提供对以资源为中心的世界的人工转换。另一方面,RPC非常适合面向操作的api,在这种api中,您公开的是服务,而不是可用于RPC的资源集。

毫无疑问REST更受欢迎,如果你想将API公开给第三方,这无疑会增加一些好处。

如果不是(例如在SPA中创建AJAX前端),我的选择是RPC。特别是JSON- rpc,结合JSON Schema作为描述语言,并根据用例通过HTTP或Websockets传输。

JSON-RPC是一个简单而优雅的规范,它定义了用于同步或异步RPC的请求和响应JSON有效负载。

JSON Schema是一份规范草案,定义了一种基于JSON的格式,旨在描述JSON数据。通过使用JSON Schema描述您的服务输入和输出消息,您可以在消息结构中拥有任意的复杂性,而不会影响可用性,并且可以自动化服务集成。

传输协议(HTTP vs websockets)的选择取决于不同的因素,最重要的是你是否需要HTTP特性(缓存、重新验证、安全性、幂等性、内容类型、多部分等等),或者你的应用程序是否需要在高频率下交换消息。

到目前为止,这主要是我个人对这个问题的看法,但是现在有一些东西可以真正帮助那些阅读这些行的Java开发人员,我在过去的一年里一直在工作的框架,诞生于你现在想知道的同一个问题:

http://rpc.brutusin.org

你可以在这里看到一个现场演示,展示了用于功能测试的内置存储库浏览器(感谢JSON Schema)和一系列示例服务:

http://demo.rpc.brutusin.org

希望对伴侣有帮助!

Nacho

如果您请求资源,那么RESTful API在设计上更好。如果您请求一些具有大量参数和复杂方法的复杂数据,而不是简单的CRUD,那么RPC是正确的方法。

我认为,一如既往,这取决于……

REST具有广泛的公众支持的巨大优势,这意味着有大量的工具和书籍。如果你需要制作一个API,让来自不同组织的大量消费者使用,那么只有一个原因:它是受欢迎的。作为一种协议,它当然是完全失败的,因为有太多完全不同的方法来将命令映射到URL/动词/响应。

因此,当你编写一个需要与后端对话的单页web应用程序时,我认为REST太复杂了。在这种情况下,你不必担心长期的兼容性,因为应用程序和API可以一起发展。

I once started with REST for a single page web app but the fine grained commands between the web app and the server quickly drove me crazy. Should I encode it as a path parameter? In the body? A query parameter? A header? After the URL/Verb/Response design I then had to code this mess in Javascript, the decoder in Java and then call the actual method. Although there are lots of tools for it, it is really tricky to not get any HTTP semantics in your domain code, which is really bad practice. (Cohesion)

尝试为中等复杂的站点制作一个Swagger/OpenAPI文件,并将其与该文件中描述远程过程的单个Java接口进行比较。复杂性的增长是惊人的。

因此,对于单页web应用程序,我从REST切换到JSON-RPC。aI开发了一个小型库,在服务器上编码Java接口,并将其传送到浏览器。在浏览器中,这为为每个函数返回承诺的应用程序代码创建了一个代理。

同样,REST也有它的地位,因为它很有名,因此得到了很好的支持。认识到底层的无状态资源哲学和层次模型也很重要。然而,这些原则同样可以在RPC模型中使用。JSON RPC工作于HTTP之上,因此它在这方面具有与REST相同的优势。不同之处在于,当您不可避免地遇到这些不能很好地映射到这些原则的函数时,您不必被迫做大量不必要的工作。

根据Richardson成熟度模型,问题不是REST vs. RPC,而是多少REST?

从这个角度来看,REST标准的遵从性可以分为4个级别。

0级:从动作和参数的角度考虑。正如本文所解释的,这在本质上等同于JSON-RPC(本文对XML-RPC进行了解释,但两者的参数相同)。 第一级:从资源的角度考虑。与资源相关的所有内容都属于同一个URL 第2级:使用HTTP动词 第三级:HATEOAS

According to the creator of REST standard, only level 3 services can be called RESTful. However, this is a metric of compliance, not quality. If you just want to call a remote function that does a calculation, it probably makes no sense to have relevant hypermedia links in the response, neither differentiation of behavior based on the HTTP verb used. So, a such call inherently tends to be more RPC-like. However, lower compliance level does not necessarily mean statefulness, or higher coupling. Probably, instead of thinking REST vs. RPC, you should use as much REST as possible, but no more. Do not twist your application just to fit with the RESTful compliance standards.

RPC的基本问题是耦合。RPC客户端以多种方式与服务实现紧密耦合,在不破坏客户端的情况下更改服务实现变得非常困难:

客户需要知道过程名称; 程序参数顺序,类型和计数事项。在不破坏客户端实现的情况下,在服务器端改变过程签名(参数的数量、参数的顺序、参数类型等)并不是那么容易的; RPC样式只公开过程端点+过程参数。客户不可能决定下一步该做什么。

另一方面,在REST风格中,通过在表示(HTTP报头+表示)中包含控制信息来引导客户端是非常容易的。例如:

It's possible (and actually mandatory) to embed links annotated with link relation types which convey meanings of these URIs; Client implementations do not need to depend on particular procedure names and arguments. Instead, clients depend on message formats. This creates possibility to use already implemented libraries for particular media formats (e.g. Atom, HTML, Collection+JSON, HAL etc...) It's possible to easily change URIs without breaking clients as far as they only depend on registered (or domain specific) link relations; It's possible to embed form-like structures in representations, giving clients the possibility to expose these descriptions as UI capabilities if the end user is human; Support for caching is additional advantage; Standardised status codes;

在REST方面还有更多的区别和优势。