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

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


当前回答

为什么JSON RPC:

对于REST api,我们必须为可能需要的每个功能/方法定义一个控制器。因此,如果我们想让客户端访问10个方法,我们就必须编写10个控制器来将客户端请求连接到特定的方法。

另一个因素是,即使我们为每个方法/功能使用不同的控制器,客户端也必须记住是使用POST还是GET。这使事情更加复杂。为了发送数据,如果使用POST,则必须设置请求的内容类型。

对于JSONRPC,事情大大简化了,因为大多数JSONRPC服务器操作POST HTTP方法,内容类型始终是application/ JSON。这减轻了在客户端使用正确的HTTP方法和内容设置的负担。

不必为服务器想要向客户端公开的不同方法/功能创建单独的控制器。

为什么休息:

对于服务器希望向客户端公开的不同功能,有不同的url。因此,您可以嵌入这些url。

这些观点大多是有争议的,完全取决于一个人的需要。

其他回答

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方面还有更多的区别和优势。

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

我使用vdata为RPC协议: http://vdata.dekuan.org/

1、PHP和JavaScript都没问题。 2、跨源资源共享(CORS)呼叫仍然正常。

在我看来,关键在于行动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

最好在REST和JSON-RPC之间选择JSON-RPC,为web应用程序开发一个更容易理解的API。JSON-RPC是首选,因为它对方法调用和通信的映射很容易理解。

选择最合适的方法取决于约束条件或主要目标。例如,只要性能是一个主要特征,就建议使用JSON-RPC(例如,高性能计算)。但是,如果主要目标是不可知,以便提供一个通用的接口供其他人推断,则建议使用REST。如果两个目标都需要实现,建议同时包含两个协议。

The fact which actually splits REST from JSON-RPC is that it trails a series of carefully thought out constraints- confirming architectural flexibility. The constraints take in ensuring that the client as well as server are able to grow independently of each other (changes can be made without messing up with the application of client), the calls are stateless (the state is regarded as hypermedia), a uniform interface is offered for interactions, the API is advanced on a layered system (Hall, 2010). JSON-RPC is rapid and easy to consume, however as mentioned resources as well as parameters are tightly coupled and it is likely to depend on verbs (api/addUser, api/deleteUser) using GET/ POST whereas REST delivers loosely coupled resources (api/users) in a HTTP. REST API depends up on several HTTP methods such as GET, PUT, POST, DELETE, PATCH. REST is slightly tougher for inexperienced developers to implement.

JSON (denoted as JavaScript Object Notation) being a lightweight data-interchange format, is easy for humans to read as well as write. It is hassle free for machines to parse and generate. JSON is a text format which is entirely language independent but practices conventions that are acquainted to programmers of the family of languages, consisting of C#, C, C++, Java, Perl, JavaScript, Python, and numerous others. Such properties make JSON a perfect data-interchange language and a better choice to opt for.