How to better design your REST API's restful architecture-based Web service design and Rest framework implementation

Source: Internet
Author: User
Tags representational state transfer

First, REST

With status Transfer (English:representational State Transfer, short REST) is Dr. Roy Fielding a software architecture style proposed by his doctoral dissertation in the year.

The

is now in the three mainstream web service implementation scenario. Because rest mode with complex soap and xml-rpc web service was started with the Span style= "Font-family:times New Roman" >rest style design and implementation.

For example: Amazon.com provides close -to-REST-style Web Services for book searches, and Yahoo provides web Services REST -style.

II. Architecture Design for rest

  REST ( Representational State Transfer ) is a lightweight Web Service architectural style, in fact, and operation significantly more than SOAP and the Xml-rpc more concise, able to completely pass HTTP protocol implementation, and the ability to leverage caching Cache to improve response times. Performance, efficiency, and ease of use are superior to the SOAP protocol.

  REST Architecture follows the CRUD principles, CRUD The principle requires only four kinds of behavior for resources: Create (Create), Read (read), Update (update) and Delete (delete) will be able to complete its operation and processing. These four operations are an atomic operation. An operation that can no longer be divided. They are able to construct complex operating procedures, just as mathematically arithmetic are the most important operations of numbers.

   RESTarchitecture gives people a real understanding of our network protocolsHTTPoriginal Look, the operation of the resource contains the operation to get, create, alter, and delete the resource exactly as appropriateHTTPthe agreement providesGET,POST,PUTand theDELETEmethod, soRESTputHTTPto aURLthe operation of the resource is limited toGET,POST,PUTand theDELETEwithin four of these.

This approach is designed and developed for Web applications. Can reduce the complexity of development. Improve the scalability of the system.

Iii. Design Guidelines for rest

  REST architecture is for Web The purpose of this application is to reduce the complexity of development and to improve the scalability of the system. REST presents design guidelines such as the following:

L All things on the network are abstracted as resources (resource)

• A unique resource identifier for each resource (resource identifier)

• Operation of the resource via a common Connector interface (generic connector Interface)

• Various operations on resources do not change resource identifiers

L All operations are stateless (stateless)

Iv. key points and standards

It is important to note that. REST is a design style rather than a standard.

REST typically based on the use HTTP . URIs, and XML as well as HTML These existing widely popular protocols and standards.

The resource is specified by the URI .

The operation of the resource includes acquiring, creating, altering, and deleting resources, which are exactly what the HTTP Protocol provides, the get,POST,PUT and DELETE methods.

L manipulate resources by manipulating their representations.

The expression of the resource is either XML or HTML. Depends on whether the reader is a machine or a person. Is the customer software for the consumer Web service or the Web Browser.

Of course it can be in whatever other format.

V. Requirements for REST

L Client and server structure

L Connectivity Protocol has no status

L can use the Cache mechanism to improve performance

• Hierarchical System

L On Demand Code-Javascript ( optional)

Vi. about the State

You should pay attention to the status of the differential application and the status of the Connection Agreement. HTTP The connection is stateless (that is, no information is logged for each connection), and REST The transfer includes all the state information of the app, so it can significantly reduce the HTTP the connection's repeated request resource consumption.

Vii. Web Services with state transport

A web service with state transport (also known as a restful web API) is a use of HTTP and follows the REST Principles of Web service.

It is defined from the following three aspects of a resource:

• Intuitive short resource address:URI, for example:http://example.com/resources/.

L Transport Resources:Web Services Accept and return the Internet media type, for example:JSON. XML ,YAML , and so on.

Operations on resources: a series of request methods supported by the WEB service on this resource (for example:POST,GET, PUT or DELETE).

The following table lists the typical uses of the HTTP request method When implementing a Web service with state transport .

Typical application of the HTTP request method in RESTful Web Services

Resources

GET

PUT

POST

DELETE

A URIfor a set of resources. For example http://example.com/resources/

Lists the URIand thespecific information for each resource in the resource group (which is optional).

Replaces the current entire set of resources with a given set of resources.

Create / append a new resource in this group of resources.

This operation often returns the URLof the new resource.

Deletes the entire set of resources.

URIfor a single resource, for example http://example.com/resources/142

Gets The specific information for the specified resource. Format can choose a suitable network media type (for example:XML,JSON , etc.)

Replace / create the specified resource.

and append it to the corresponding resource group.

The specified resource is treated as a resource group and under which a new element is created / appended to be subordinate to the current resource.

Deletes the specified element.

The PUT and DELETE methods are idempotent. GET The method is a security method (there is no change to the server side, so it is, of course, idempotent).

Unlike SOAP - based Web Services.  [2]the RESTful Web service does not have a "formal" standard . This is because REST is a schema. and SOAP is just a protocol.

While rest is not a standard, you can use a variety of other standards when implementing restful Web services (for example, HTTP,URL . XML. PNG , etc.).

Viii. Examples of implementation

Like what. A simple Web Store app,

List all products,

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

To present a commodity,

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

Order Purchase,

POST Http://www.store.com/order

<purchase-order>

<item> ... </item>

</purchase-order>

Ix. Advantages of rest

L can use cache more efficiently to improve response speed

The stateless nature of the communication itself enables different servers to process different requests in a series of requests, increasing the scalability of the server

L browser can be used as client to simplify software requirements

Relative to other mechanisms superimposed on HTTP protocols,REST is less dependent on software

L no need for additional resource discovery mechanisms

L Long-term compatibility in software technology evolution is better

X. Personal Summary

The REST architecture is merely a thought, and does not limit any technology, no matter what language.

The essence of REST is HTTP invocation, which reduces the degree of coupling between the various applications.

L Good REST architecture. There should be a unified representation and data format, can effectively organize the various resources, and can be effectively controlled.


How to better design your REST API's restful architecture-based Web service design and Rest framework implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.