What does “state transfer” in Representational Sta

2019-02-01 22:39发布

问题:

What does the State Transfer in Representational State Transfer refer to?

Found some explanations about this (e.g. here) but I still don't understand. For example in the article it is said

The representation places the client application in a state.

Why? What does state (as I understand it, something like a session) have to do with a representation of a resource?

回答1:

Why? What does state (as I understand it, something like a session) have to do with a representation of a resource?

An object has attributes (or state) and behaviors (or methods).

If I want to move an object from my desktop to a server I have to do the following:

  1. Create a representation of the state of the object.

  2. Transfer that representation from the desktop to the server.

The methods I don't transfer. I install the same class definition on both machines.

So, REST is about creating a representation of the object's current state so it can be transferred to another server from which the object can be reconstructed.

We only send the state -- the attributes -- of the object. And we have to create an external, serialized representation of that state.



回答2:

Consider a client application to be a kind of giant state machine. The client's initial state is equal to the first representation returned from the server. Links in the returned representation provide possible "state transitions".

One think to note is that there are a two major types of links, passive and active. Passive links like <img> and <link rel="stylesheet"> do not actually cause a state transition, they simply augment the current state. Links like <form> and <a> however are active links and the cause a state transition. After following one of these links the new client state is equal to the returned representation, aka state transfer.

If you are used to building traditional desktop client applications you will find this is a radically different architecture. Not one that you are likely to grok overnight. Initially this approach may seem very limiting but when you consider that a client application can be the host to many simultaneously executing state machines you will start to realize that you can do just about anything that you could using a remote object architecture and still maintain the loose coupling of web browser.



回答3:

The Web is comprised of resources. A resource is any item of interest. For example, theBoeing Aircraft Corp may define a 747 resource. Clients may access that resource with thisURL:http://www.boeing.com/aircraft/747A representation of the resource is returned (e.g., Boeing747.html). The representation places the client application in a state. The result of the client traversing a hyperlink inBoeing747.html is another resource is accessed. The new representation places the client application into yet another state. Thus, the client application changes (transfers) state with each resource representation --> Representational State Transfer!