Research on Rest-style web Services

Source: Internet
Author: User
Tags representational state transfer

Summary: Web services have been around for more than a decade since their birth. People use them to solve interoperability problems between different platforms. Traditional Web services are based on RPC, and their implementation technologies mainly include SOAP and WS standard stacks. RPC-style Web services are relatively closed. problems such as complicated technical architecture and poor scalability will occur in the distributed and open Web environments. The REST-style Web services studied are sufficient to solve the above problems.

Keywords: Rest, architecture, web Service, HTTP, URI

Introduction: the rest style is formally proposed by Roy Thomas Fielding, an HTTP protocol writer, in his doctoral thesis. It is a modern Web architecture model used to guide Web design, definition, and deployment. REST can also be said to be the design principle behind the basic protocols of the HTTP and URI Web architectures. Designing the Web application architecture based on the REST style is the best practice to maximize the potential of HTTP and URI. In fact, when HTTP was invented, REST already exists, but the later Web development model gradually deviated from the nature of HTTP and finally pursued the RPC model. Web Services built using the REST style can make full use of the Web features to solve the problems brought about by traditional Web Services.

1. Introduction to the Rest Architecture

REST (Representational State Transfer) is a concept proposed in Roy Feilding's doctoral thesis. It is a description of the Web system architecture design principles and a resource-based service access architecture. Roy Feilding defines REST in his paper: the purpose of REST is to determine how to move a well-designed Web program forward, you can select a hyperlink on a Web page with a hyperlink (representing State Migration) to make another Web page (representing the next state of the program) return to the user, so that the Web program can be further run. For example, if a customer requests a ticket purchasing service from a railway station and the return result page contains two links: Train shift and timetable, the two links are the tables (Representational) that provide resources to the server ). After the returned results are returned, the customer selects a link to determine the next action, which means that the customer's status also changes with the migration. In this way, the customer can maintain their own program status.

2. Relationship between REST and CRUD principles

The REST Software Architecture follows the CRUD principle, which tells us that only four actions are required for resources (including network resources): Create, Read, and Update) and DELETE. In fact, all things in the world follow this rule: Generate, change, see, and destroy. Therefore, the computer world is no exception. This principle is derived from our data operations on database tables: insert, select, update, and delete ), so sometimes CRUD is written as RUDI, where I is insert. These four operations are an atomic operation, that is, an operation that cannot be further divided. They can be used to construct complex operation processes, just as the four arithmetic operations in mathematics are the most basic arithmetic operations of numbers.

3. Construct a REST-based Web Service

Web Service technology allows applications to communicate with each other in an independent platform or programming language. However, traditional XML-RPC-style Web Services are gradually threatened by complexity demons. REST-style Web Services provide a simpler alternative.

In a REST-based Web system, all resources have a URI, including Web services, which are also identified by Uris. The URI used to identify the resource is preferably a logical URI (for example, a http://www.anyshop.com/5686), rather than a physical URI (http://www.anyshop.com/5686.html ).

The advantage of using logical URI is that modification to server resources does not affect the customer's use. Http cet, POST, PUT, and DELEIT actions are used as common interfaces for resources. Users can access resources through them. Here, HTTP is used as a program protocol, rather than as a transmission protocol for transmitting a SOAP message.

Taking the Web service of a part warehouse as an example to explain the basic idea of REST Web service. The parts warehouse deployed on the server releases some external services, so that the customer can:

L obtain a component list.

L obtain detailed information about a specific component.

L submit a purchase list (OP ).

L retrieve component list

Since REST-based Web services can be accessed through a URI, it is very simple to call a Web service. The customer needs to obtain a list of parts in the Order1 order. If there is a Web Service PartsList to implement this function, you can use http://www.anycorp.com/partslistto get the part rolling. The customer only needs to call this method. It is transparent to the customer as to how the server is implemented. Because it is transparent to customers, the server can freely modify resources without affecting customers, thus achieving loose coupling.

After the customer knows that the previous URL is used, the server will return a document containing the part list, namely:

<? Xml version "1.0">

<P: Parts xmlns: p = "http://www.anycorp.com"

Xmlns: xlink = "http://www.w3.org/5o/xlink”>

<Part id = "00345" xlink: href = "http://www.anycorp.com/partslist/00345">

<Part id = "00346" xlink: href = "http://www.anycorp.com/partslist/00133">

</P: Parts>

The returned document contains links to each part. The customer can select a proper link to migrate the resources that the link points to the customer to achieve self-maintenance of the customer's status. This is a key feature of REST. Obtains detailed information about a specific component.

If you want to return detailed information for each part in XML format, you only need to add the part number parameter after the URL, such as http://www.anycorp.com/partslist/00345. The following document will be returned:

<? Xml version = "1.0">

<P: Part xmlns: p = "http://www.parts-depot.com"

Xmlns: xlink = "http://www.w3.org/5o/xlink”>

<Part-ID> 00345 </Part-ID>

<Name> Widget-A </Name>

<Description> This part is used within the frap assembly </Description>

<Specification xlink: href = "http://www.anycorp.com/partslist/00345/specification”"/>

<UnitCost currency = "USD"> 0.10 </UnitCost>

<Quantity> 10 </Quantity>

</P: Part>

From the data returned above, this data is linked to more data. Customers can continue to select these links to obtain more detailed data, which is the key to REST.

Submit a purchase list (OP), which uses a URL pointing to the PO. The customer creates a PO instance document according to the PO format pre-defined in the part warehouse. The customer submits PO. xml as the content carried by http post. The PO service responds to this PO submission request, so that the customer can retrieve, update, and edit this PO at any time. PO becomes a piece of information shared between the client and the server. The shared information (PO) is accessed by a URL specified by the server and exposed as a Web service.

4. REST Web service design principles

(1) The key to building Web Services with REST is to correctly identify the resources used as services.

(2) provide a URL for each resource. The resource should be a noun, not a verb.

(3) Based on the customer's resource processing method, the customer can be divided into the performance of only receiving resources and the ability of the customer to modify (ADD) resources. For the former, http get is provided, and for the latter, http post, PUT, and DELETE are provided.

(4) All resources accessed through http cet should be unchangeable. That is to say, these resources only return the performance of one resource to the customer, and the customer calls them and cannot change them.

(5) No isolated performance. In other words, the link is provided in the resource performance to allow customers to obtain more information or related information.

(6) Provide data gradually. Instead of providing all the content in a document, you can provide links to more detailed information.

Because REST is a description of today's Web architecture design principles, the goals and principles of REST are a summary of the elements that have been successfully applied in today's Web.

4. Summary

This article describes the design principles and features of REST-based Web Services and the idea of creating REST-based Web Services. By understanding REST, REST will inevitably have an important impact on the Web service architecture. REST-based Web Services will certainly promote the large-scale application and further development of the current Web Service technology.

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.