REST 英文全称是 REpresentational State Transfer,其中的 state 指的是什么?representational 修饰的是 state,还是 state transfer?

关注者
24
被浏览
4,303
登录后你可以
不限量看优质回答私信答主深度交流精彩内容一键收藏

The short version:

  1. state指的是资源在某个时刻的状态
  2. representational修饰的是state

The longer version:

关于REST的解释,我们可以从Roy Fielding的论文Architectural Styles and the Design of Network-based Software Architectures中找到答案。

参考5.2.1.2 Representations:

REST components perform actions on a resource by using a representation to capture the current or intended state of that resource and transferring that representation between components.

从这段话我们可以看出,representation是资源当前或者预期状态的一个展现形式。这意味着,某个时刻的状态是可以用某种形式展现出来的,也就是说,State是Representational的。所以说,Representational是用来修饰State的。

参考6.1 Standardizing the Web:

The name "Representational State Transfer" is intended to evoke an image of how a well-designed Web application behaves: a network of web pages (a virtual state-machine), where the user progresses through the application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.

这段话告诉我们,在Web应用中,用户通过点击链接跳转,实现了state的变化,所以在Web应用中,state指网页或者网页这种展现对应的资源在当时的状态。而在更抽象的REST中,state指资源在某个时刻的状态。

关于资源和状态的关系,参考5.2.1.1 Resources and Resource Identifiers:

More precisely, a resource R is a temporally varying membership function MR(t), which for time t maps to a set of entities, or values, which are equivalent. The values in the set may be resource representations and/or resource identifiers.

在某一个时刻的资源即为一个状态,一个状态对应一组展现和/或者一组资源标识(子资源)。

以上为个人理解,欢迎讨论。