Web service implementation scheme 1: Introduction to REST

 

Reference: https://en.wikipedia.org/wiki/REST

 

REST: Representational State Transfer (English: Representational State Transfer, REST for short)

 

Key points and standards

  • It is important to note that REST is a design style and not a standard. REST is generally based on the use of HTTP, URI, and XML and HTML, these existing widely popular protocols and standards.
  • Resources are specified by URIs.
  • Operations on resources include acquiring, creating, modifying, and deleting resources, and these operations correspond to the GET, POST, PUT, and DELETE methods provided by the HTTP protocol.
  • Manipulate resources by manipulating their representation.
  • The representation of the resource is either XML or HTML, depending on whether the reader is a machine or a human, client software consuming the web service, or a web browser. Of course any other format is also possible.

 

Requirements for REST

  • Client and server structure: Communication can only be initiated unilaterally by the client, in the form of a request-response.
  • The connection protocol is stateless: the session state of the communication should be maintained entirely by the client.
  • Ability to use the Cache mechanism to improve performance: Response content can be cached somewhere in the communication chain to improve network efficiency.
  • Consistent operation interface: The components of the communication chain communicate with each other through a unified interface to improve the visibility of the interaction.
  • Hierarchical system: Decomposes the architecture into several levels of layers by restricting the behavior of the components (i.e. each component only "sees" the immediately adjacent layers with which it interacts).
  • Required Code - Javascript (optional): Supports extension of client-side functionality by downloading and executing some code (eg Java Applet, Flash or JavaScript)

 

About status

Care should be taken to distinguish between the state of the application and the state of the connection protocol. HTTP connections are stateless (that is, information about each connection is not recorded), and REST transmissions contain all the state information of the application, which can greatly reduce the resource consumption of repeated requests for HTTP connections.

 

Applied to Web Services

  • A web API that conforms to the REST design style is called a RESTful API. It is defined from the following three sources:
  • Intuitive and short resource address: URI, for example: http://example.com/resources/.
  • Transmitted resources: Internet media types accepted and returned by the web service, such as JSON, XML, YAML, etc.
  • Operations on the resource: A set of request methods (eg POST, GET, PUT or DELETE) supported by the web service on the resource.

 

The following table lists typical uses of HTTP request methods when implementing a RESTful API.

[1] http://example.com/resources/ http://example.com/resources/142 Typical application of HTTP request methods in RESTful API Resource GET PUT POST DELETE URI of a group of resources, such as the URI of a single resource, such as
Lists the URI, along with details of each resource in this resource group (the latter is optional). Replaces the current entire set of resources with the given set of resources. Create/append a new resource to this group of resources. This operation often returns the URL of the new resource. Delete the entire set of resources.
Get the detailed information of the specified resource, the format can choose a suitable network media type (such as: XML, JSON, etc.) Replace/create the specified resource. and append it to the appropriate resource group. Treat the specified resource as a resource group, and create/append a new element under it to make it belong to the current resource. Removes the specified element.

PUT和DELETE方法是幂等方法。GET方法是安全方法(不会对服务器端有修改,因此当然也是幂等的),GET方法具有幂等性[指多个相同请求返回相同的结果]——如果你发送了一个GET请求没有得到结果,你可能不知道原因是请求未能到达目的地,还是响应在反馈的途中丢失了。幂等性保证了你可以简单地再发送一次请求解决问题。

 

 

不像基于SOAP的Web服务,RESTful Web服务并没有“正式”的标准。这是因为REST是一种架构,而SOAP只是一个协议。虽然REST不是一个标准,但大部分RESTful Web服务实现会使用HTTP、URI、JSON和XML等各种标准

 

实现举例

例如,一个简单的网络商店应用,列举所有商品,

GET http://www.store.com/products

呈现某一件商品,

GET http://www.store.com/product/12345

下单购买,

POST http://www.store.com/order
<purchase-order>
  <item> ... </item>
</purchase-order>

REST的优点

  • 可更高效利用缓存来提高响应速度
  • 通讯本身的无状态性可以让不同的服务器的处理一系列请求中的不同请求,提高服务器的扩展性
  • 浏览器即可作为客户端,简化软件需求
  • 相对于其他叠加在HTTP协议之上的机制,REST的软件依赖性更小
  • 不需要额外的资源发现机制
  • 在软件技术演进中的长期的兼容性更好 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852673&siteId=291194637