我需要一些澄清。我一直在阅读关于REST和构建基于REST的应用程序的书籍。根据维基百科,REST本身被定义为具象状态传输。因此,我不理解所有这些无国籍的官样文章,每个人都在不停地吐出来。

从维基百科:

在任何特定的时间,客户端可以处于之间的转换 应用程序处于“静止”状态。处于休息状态的客户端可以 与用户交互,但不创建负载,也不消耗每个客户端 服务器集或网络上的存储。

他们只是说不要使用会话/应用程序级数据存储吗?

我知道REST的一个目标是使URI访问一致且可用,例如,不是在帖子中隐藏分页请求,而是使请求的页码成为get URI的一部分。对我来说很有道理。但是,说每个客户端数据(会话数据)都不应该存储在服务器端似乎有点过头了。

如果我有一个消息队列,而我的用户想要读取消息,但在读取消息时,又想在会话期间阻止某些发送者的消息通过,该怎么办?将其存储在服务器端的某个位置,并让服务器只发送未被用户阻止的消息(或消息ID),这难道不合理吗?

每次请求新的消息列表时,我真的必须发送整个消息发送者列表来阻止吗?与我相关的消息列表在一开始就不应该是公开可用的资源。

我只是想理解一下。谁来澄清一下。


更新:

我发现了一个堆栈溢出的问题,它的答案并不能让我一直走到那里: 如何在REST中管理状态 这说明客户端状态很重要,应该在每个请求上都传输....Ugg . .看起来开销很大…这样对吗??


当前回答

Stateless means the state of the service doesn’t persist between subsequent requests and response. Each request carries its own user credentials and is individually authenticated. But in stateful each request is known from any prior request. All stateful requests are session-oriented i.e. each request need to know and retain changes made in previous requests. Banking application is an example of stateful application. Where user first login then make transaction and logs out. If after logout user will try to make the transaction, he will not be able to do so. Yes, http protocol is essentially a stateless protocol but to make it stateful we make us of HTTP cookies. So, is SOAP by default. But it can be make stateful likewise, depends upon framework you are using. HTTP is stateless but still we can maintain session in our java application by using different session tracking mechanism. Yes, We can also maintain session in webservice whether it is REST or SOAP. It can be implemented by using any third party library or you can implement by our own.

摘自http://gopaldas.org/webservices/soap/webservice-is-stateful-or-stateless-rest-soap

其他回答

REST非常抽象。有一些好的、简单的、真实的例子是有帮助的。

以所有主要的社交媒体应用程序为例——Tumblr、Instagram、Facebook和Twitter。它们都有一个永远滚动的视图,你往下滚动得越远,你看到的内容就越多,时间也越来越久远。然而,我们都经历过这样的时刻,你失去了你滚动到的位置,应用程序将你重置回顶部。比如,如果你退出了应用程序,那么当你重新打开它时,你又回到了顶部。

原因是服务器没有存储您的会话状态。遗憾的是,您的滚动位置只是存储在客户端的RAM中。

幸运的是,重新连接时不必重新登录,但这只是因为客户端存储的登录证书还没有过期。删除并重新安装应用程序,你将不得不重新登录,因为服务器没有将你的IP地址与你的会话关联。

服务器上没有登录会话,因为它们遵循REST。


现在,上面的例子根本不涉及web浏览器,但在后端,应用程序通过HTTPS与它们的主机服务器通信。我的观点是REST不必涉及cookie和浏览器等。存储客户端会话状态的方法多种多样。

但是让我们来讨论一下web浏览器,因为它带来了REST的另一个主要优势,这里没有人谈论这个优势。

如果服务器试图存储会话状态,它应该如何识别每个单独的客户机?

它不能使用他们的IP地址,因为许多人可以在共享路由器上使用相同的地址。那怎么做呢?

它不能使用MAC地址的原因有很多,最重要的是你可以在不同的浏览器和应用程序上同时登录多个不同的Facebook账户。一个浏览器可以很容易地伪装成另一个浏览器,MAC地址也很容易被欺骗。

If the server has to store some client-side state to identify you, it has to store it in RAM longer than just the time it takes to process your requests, or else it has to cache that data. Servers have limited amounts of RAM and cache, not to mention processor speed. Server-side state adds to all three, exponentially. Plus if the server is going to store any state about your sessions then it has to store it separately for each browser and app you're currently logged in with, and also for each different device you use.


所以…我希望您现在明白为什么REST对于可伸缩性如此重要。 我希望您可以开始理解为什么服务器端会话状态之于服务器可伸缩性,就像焊接铁砧之于汽车加速。


人们感到困惑的地方是认为“状态”指的是存储在数据库中的信息。不,它指的是当您使用服务器RAM时需要存储在它中的任何信息。

The whole concept is different... You don't need to manage sessions if you are trying to implement RESTFul protocol. In that case it is better to do authentication procedure on every request (whereas there is an extra cost to it in terms of performance - hashing password would be a good example. not a big deal...). If you use sessions - how can you distribute load across multiple servers? I bet RESTFul protocol is meant to eliminate sessions whatsoever - you don't really need them... That's why it is called "stateless". Sessions are only required when you cannot store anything other than Cookie on a client side after a reqest has been made (take old, non Javascript/HTML5-supporting browser as an example). In case of "full-featured" RESTFul client it is usually safe to store base64(login:password) on a client side (in memory) until the applictation is still loaded - the application is used to access to the only host and the cookie cannot be compromised by the third party scripts...

我强烈建议禁用RESTFul服务的cookie认证…检查Basic/Digest Auth -这对于基于rest的服务应该足够了。

您必须在客户端管理客户端会话。这意味着您必须在每个请求中发送身份验证数据,并且您可能(但不一定)在服务器上有一个内存缓存,它将身份验证数据与用户信息(如身份、权限等)配对。

这种REST无状态约束非常重要。如果不应用此约束,您的服务器端应用程序将不能很好地扩展,因为维护每个客户机会话将是它的阿喀琉斯之踵。

用户应用程序状态管理的历史视图

传统意义上的会话将用户的状态保存在服务器内部的应用程序中。这可能是流中的当前页面,也可能是先前已输入但尚未持久化到主数据库中的内容。

这种需求的原因是客户端缺乏有效维护状态的标准,而不需要特定于客户端(即特定于浏览器)的应用程序或插件。

随着时间的推移,HTML5和XML Header Request已经标准化了在客户端(即浏览器端)以标准方式存储包括应用程序状态在内的复杂数据的概念,而无需在服务器之间来回传输。

REST服务的一般使用

REST服务通常在需要执行事务或需要检索数据时调用。

REST服务应该由客户端应用程序调用,而不是由最终用户直接调用。

进行身份验证

对于任何对服务器的请求,请求的一部分应该包含授权令牌。它是如何实现的是特定于应用程序的,但通常是BASIC或CERTIFICATE形式的身份验证。

Form based authentication is not used by REST services. However, as noted above REST services are not meant to be called by the user, but by the application. The application needs to manage getting the authentication token. In my case I used cookies with JASPIC with OAuth 2.0 to connect to Google for authentication and simple HTTP Authentication for automated testing. I also used HTTP Header authentication via JASPIC for local testing as well (though the same approach can be performed in SiteMinder)

根据这些示例,身份验证是在客户端进行管理的(尽管SiteMinder或谷歌将在其端存储身份验证会话),对于该状态无法做任何事情,但它不是REST服务应用程序的一部分。

检索请求

REST中的检索请求是GET操作,其中请求特定的资源并且是可缓存的。不需要服务器会话,因为请求拥有检索数据所需的一切:身份验证和URI。

事务脚本

如上所述,客户端应用程序本身调用REST服务以及它在客户端管理的身份验证。

这对于REST服务(如果操作正确的话)意味着将单个请求发送到REST服务器,该服务器将包含单个用户操作所需的所有内容,该操作执行单个事务所需的所有内容,事务脚本就是该模式的名称。

这通常通过POST请求来完成,但也可以使用其他请求,如PUT。

很多人为的REST示例(我自己做过)试图尽可能多地遵循HTTP协议中定义的内容,在经历了这些之后,我决定更加务实,只把它留给GET和POST。POST方法甚至不需要实现POST- redirect - get模式。

尽管如此,正如我上面提到的,客户端应用程序将是调用服务的应用程序,它只在需要时(不是每次)调用带有所有数据的POST请求。这可以防止对服务器的不断请求。

轮询

尽管REST也可以用于轮询,但我不推荐使用它,除非出于浏览器兼容性的考虑必须使用它。为此,我将使用WebSockets,我也为它设计了API契约。旧浏览器的另一个替代方案是CometD。

Stateless means the state of the service doesn’t persist between subsequent requests and response. Each request carries its own user credentials and is individually authenticated. But in stateful each request is known from any prior request. All stateful requests are session-oriented i.e. each request need to know and retain changes made in previous requests. Banking application is an example of stateful application. Where user first login then make transaction and logs out. If after logout user will try to make the transaction, he will not be able to do so. Yes, http protocol is essentially a stateless protocol but to make it stateful we make us of HTTP cookies. So, is SOAP by default. But it can be make stateful likewise, depends upon framework you are using. HTTP is stateless but still we can maintain session in our java application by using different session tracking mechanism. Yes, We can also maintain session in webservice whether it is REST or SOAP. It can be implemented by using any third party library or you can implement by our own.

摘自http://gopaldas.org/webservices/soap/webservice-is-stateful-or-stateless-rest-soap