我试图在REST和JSON-RPC之间做出选择,为web应用程序开发API。它们是如何比较的?
更新2015:我发现REST在Web/HTTP上的API上更容易开发和使用,因为API可以利用客户端和服务器都理解的现有和成熟的HTTP协议。例如,API不需要任何额外的工作或设置就可以使用响应代码、报头、查询、帖子正文、缓存和许多其他特性。
我试图在REST和JSON-RPC之间做出选择,为web应用程序开发API。它们是如何比较的?
更新2015:我发现REST在Web/HTTP上的API上更容易开发和使用,因为API可以利用客户端和服务器都理解的现有和成熟的HTTP协议。例如,API不需要任何额外的工作或设置就可以使用响应代码、报头、查询、帖子正文、缓存和许多其他特性。
当前回答
最好在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.
其他回答
根据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.
REST与HTTP紧密耦合,因此如果您只通过HTTP公开API,那么REST更适合于大多数(但不是所有)情况。然而,如果你需要通过其他传输方式(如消息传递或web套接字)公开你的API,那么REST就不适用了。
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方面还有更多的区别和优势。
Great answers - just wanted to clarify on a some of the comments. JSON-RPC is quick and easy to consume, but as mentioned resources and parameters are tightly coupled and it tends to rely on verbs (api/deleteUser, api/addUser) using GET/ POST where-as REST provides loosely coupled resources (api/users) that in a HTTP REST API relies on several HTTP methods (GET, POST, PUT, PATCH, DELETE). REST is slightly harder for inexperienced developers to implement, but the style has become fairly common place now and it provides much more flexibility in the long-run (giving your API a longer life).
除了没有紧密耦合的资源外,REST还允许您避免提交到单一的内容类型中——这意味着如果您的客户端需要接收XML、JSON甚至YAML格式的数据——如果在您的系统中,您可以使用内容类型/接受标头返回其中任何一种。
这让你的API足够灵活,以支持新的内容类型或客户端需求。
But what truly separates REST from JSON-RPC is that it follows a series of carefully thought out constraints- ensuring architectural flexibility. These constraints include ensuring that the client and server are able to evolve independently of each other (you can make changes without messing up your client's application), the calls are stateless (state is represented through hypermedia), a uniform interface is provided for interactions, the API is developed on a layered system, and the response is cacheable by the client. There's also an optional constraint for providing code on demand.
然而,尽管如此,大多数API都不是RESTful的(根据Fielding的说法),因为它们不包含超媒体(在响应中嵌入超文本链接,帮助导航API)。大多数api都是类似REST的,因为它们遵循大多数REST的概念,但忽略了这个约束。然而,越来越多的api正在实现这一点,它正在成为一种主流实践。
这也为您提供了一些灵活性,因为超媒体驱动的api(如Stormpath)将客户端引导到URI(这意味着如果某些情况发生了变化,在某些情况下您可以修改URI而不会产生负面影响),而与RPC一样,URI需要是静态的。使用RPC,您还需要广泛地记录这些不同的uri,并解释它们如何相互关联地工作。
一般来说,如果您想要构建一个可扩展的、灵活的、长期存在的API,我认为REST是最佳选择。基于这个原因,我认为这是99%的情况下应该走的路线。
祝你好运, 迈克
我已经对这个问题进行了一些详细的研究,并认为纯REST的局限性太大,RPC是最好的,尽管我的大多数应用程序都是CRUD应用程序。如果您坚持使用REST,那么您最终将会挠头思考如何轻松地将另一个所需的方法添加到API中以实现某些特殊目的。在许多情况下,使用REST实现这一点的唯一方法是为它创建另一个控制器,这可能会使程序过于复杂。
如果您决定使用RPC,唯一的区别是您显式地将动词指定为URI的一部分,这是清晰的、一致的、bug较少的,而且确实没有麻烦。特别是如果你要创建一个超越简单CRUD的应用程序,RPC是唯一的选择。我对REST的纯粹主义者还有另一个问题:HTTP POST, GET, PUT, DELETE在HTTP中有明确的含义,这些含义被REST颠覆为其他含义,仅仅是因为它们在大多数时间都适用——但不是所有时间都适用。
在编程中,我很久以前就发现,试图用一件事来表示两件事,有时会让你吃不厌。我喜欢能够对几乎每个操作都使用POST,因为它提供了按方法需要发送和接收数据的自由。你不能把整个世界都放进CRUD里。