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.

There are currently three main types ofWebService implementation scenario, becauseRESTPatterns and complexSOAPand theXml-rpccompared to the more concise, more and moreWebService started to takeRESTstyle design and implementation. For example: Amazon.com provides close -to-REST-style Web services for book searches; Yahoo provides web The service is also restful .

II. Architecture Design for rest

rest ( representational state transfer ) is a lightweight web service architectural style, its implementation and operation significantly more than the soap xml-rpc http protocol implementation, you can also use the cache cache soap agreement.

  REST Architecture follows the CRUD principles, CRUD The principle requires only four behaviors for resources: Create (Create), Read (read), Update (update) and Delete (delete) to complete its operation and processing. These four operations are atomic operations, an operation that can no longer be divided, through which complex operating procedures are constructed, as mathematically arithmetic are the most basic operations of numbers.

   RESTarchitecture gives people a real understanding of our network protocolsHTTPOriginal look, the operation of the resource, including the acquisition, creation, modification and deletion of resources exactly corresponds to the operationHTTPthe agreement providesGET,POST,PUTand theDELETEmethod, soRESTputHTTPto aURLthe operation of the resource is limited toGET,POST,PUTand theDELETEwithin four of these. This approach to design and development of Web-based applications can reduce the complexity of development and 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 the following design guidelines:

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

• Each resource corresponds to a unique resource identifier (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 thatREST is a design style rather than a standard. REST is typically based on the use of HTTP,URIs, and XML , and HTML These existing widely popular protocols and standards.

The resource is specified by the URI .

Operations on resources include acquiring, creating, modifying, and deleting resources that correspond exactly to the get,POST,PUT, provided by the HTTP Protocol and DELETE methods.

L manipulate resources by manipulating their representations.

The expression of the resource is either XML or HTML, depending on whether the reader is a machine or a human, a customer software that consumes Web Services or Web Browser. Of course, it can be any other format.

V. Requirements for REST

• Client and server architecture

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 be aware of the status of the application and the status of the Connection Agreement. HTTP The connection is stateless (that is, the information for each connection is not logged), and REST The transport contains all the state information for the app, so you can significantly reduce the HTTP the duplicate request resource consumption for the connection.

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 resources:

L Intuitive Short Resource address:URI, for example:http://example.com/resources/.

L Transport Resources:Web Services Accept and return Internet media types such as:JSON,XML ,YAML , etc.

Operations on resources: a series of request methods supported by WEB services on this resource (e.g.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 group of resources http://example.com/ resources/

List  uri

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

Create url

Delete whole group of resources.

A URIfor a single resource, such as http://example.com/resources/142

Gets The details of the specified resource, in the form of an appropriate network media type (e.g.XML,JSON , etc.)

Replace / create the specified resource. and append it to the appropriate 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 modification to the server side, so it is, of course, idempotent).

Not like based onSOAPof theWebServices,RESTful Webservice does not have a "formal" standard[2]. This is becauseRESTis an architecture, andSOAPjust an agreement. AlthoughRESTnot a standard, but in implementingRESTful Webother standards can be used in the service (e.g.HTTP,URL,XML,PNG, etc.).

Viii. Examples of implementation

For example, 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. Benefits of Rest

L can use cache more efficiently to improve response speed

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

L browser can be used as a client to simplify software requirements

Relative to other mechanisms superimposed on the HTTP protocol,rest is less software-dependent

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 just a thought, and does not qualify any technology, any language.

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

L Good REST Architecture, should have 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.