The REST architecture is a brand new development style for Web applications. It is the most successful distributed system architecture of Internet hypermedia in the world today. It makes people really understand the original appearance of the Http protocol. As the REST architecture becomes the mainstream technology, a brand new way of thinking for Internet network application development has become popular.
What is REST?
REST is the abbreviation of Representational State Transfer ",He is a term proposed by Dr. Roy Thomas Fielding in his paper titled neural tural Styles and the Design of Network-based Software ubuntures.REST itself is only an architectural style designed for Distributed hypermedia systems, rather than a standard.
The Web-based architecture is actually a collection of various specifications, which constitute a Web architecture. For example, the Http protocol, such as the client server mode, all of these are standards. Every time we add new specifications on the basis of the original specifications, a new architecture will be formed. While REST is such an architecture, which combines a series of specifications to form a new Web-based architecture style.
Most traditional Web applications use the B/S architecture, which includes the following specifications.
Customer-Server
This specification improves the portability of user interfaces across multiple platforms, simplifies server components, and improves system scalability. The most important thing is to separate user interfaces and data storage, so that different user terminals can enjoy the same data.
Stateless
Stateless is another layer of specification added Based on Customer-server constraints. Communication is essentially stateless, that is, every request from the customer to the server must contain all the information required to understand the request. This specification improves the visibility of the system (stateless makes it unnecessary for the client and server to save the detailed information of the other party, and the server only needs to process the current request without having to know all the request history ), reliability (stateless reduces the number of tasks that the server can recover from a local error), scalability (stateless allows the server to easily release resources, because the server does not have to save the status in multiple requests ). At the same time, the disadvantages of this specification are also obvious. Because the state data cannot be stored in the sharing context on the server, the overhead of sending duplicate data in a series of requests is increased, it seriously reduces the efficiency.
Cache
To improve the inefficiency of the stateless network, we have added cache constraints. Cache constraints allow you to implicitly or explicitly mark data in a response, so that the client can cache the response data, so that you can share the cached data for future requests, partially or entirely eliminates some interactions, increasing network efficiency. However, the information cached by the client increases the possibility of inconsistency between the client and the server, thus reducing the reliability.
The advantage of the B/S architecture is that its deployment is very convenient, but it is not ideal in terms of user experience. To improve this situation, we introduced REST.
REST adds three new specifications to the original architecture:Unified interface, hierarchical system and on-demand code.
Unified Interface
The core feature of the REST architecture style is to emphasize that there is a unified interface between components. This is manifested in the REST world, and all things on the network are abstracted as resources, while REST is to operate resources through a common linker interface.The benefit of this design is to ensure that the services provided by the system are decoupled, greatly simplifying the system, thus improving the system interaction and reusability. In addition, REST is optimized for common Web conditions, so that the REST interface is designed to efficiently transfer large-granularity hypermedia data, this causes the REST interface to be not optimal for other architectures.
Layered System
The addition of hierarchical system rules improves the independence between various layers, sets boundaries for the complexity of the entire system, and encapsulates legacy services to protect new servers from the influence of legacy clients,This improves the scalability of the system.
On-demand code
REST allows extension of client functions.For example, you can download and execute code in the form of an applet or script to expand the client functions. However, this also reduces visibility while improving system scalability. Therefore, it is only an optional constraint of REST.
REST Design Guidelines
The REST architecture is designed for Web applications to reduce development complexity and improve system scalability. REST proposes the following design principles:
Everything on the network is abstracted as a resource );
Each resource corresponds to a unique resource identifier );
Operate resources through the general connector interface (generic connector interface;
Operations on resources do not change resource identifiers;
All operations are stateless ).
Resources in REST refer not to data, but to a combination of data and representations, for example, the "10 most recently accessed members" and "10 most active members" may overlap or be identical in data, but because of their different forms, so it is classified into different resources, which is why the full name of REST is Representational State Transfer. The Resource Identifier is URI (Uniform Resource Identifier ),Whether it is an image, Word, video file, or even a virtual service, whether you are in xml format, txt file format, or other file formats, each resource is uniquely identified by a URI.
REST is based on Http, and any operations on resources are implemented through Http. In the past, most Web development used the GET and POST methods in the Http protocol, and rarely used other methods. This is actually caused by one-sided understanding of the Http protocol. Http is not only a simple protocol for carrying data, but also a network software with rich connotations. It not only uniquely identifies Internet resources, but also shows us how to operate these resources. Http limits the operation on a resource to four methods: GET, POST, PUT, and DELETE. This is the implementation of resource CRUD operations. Because resources correspond to Uris one by one, the URI does not change when performing these operations, which is very different from Web development in the past. This greatly simplifies Web development and allows the URI to be designed to reflect the resource structure more intuitively. This URI design is called a RESTful URI. The developer introduced a new way of thinking: designing the system structure through URLs. Of course, this design method is not applicable to some specific situations, that is, not all Uris can be RESTful.
REST can improve system scalability because it requires that all operations are stateless. Without Context constraints, it is easier to implement distributed and cluster operations, and the system can use the buffer Pool more effectively ). In addition, the server does not need to record a series of client accesses, which reduces the performance of the server.
Use REST Architecture
For developers, they are concerned about how to use the REST architecture. Here we will briefly discuss this issue. REST is not only a new architecture, but also a new way of thinking in the Web development process: designing the system structure through URL. In REST, all URLs correspond to resources. As long as the URL design is good, the presented system structure is also good. This is similar to Test Driven Development. It designs system interfaces through Test cases. Each Test case represents a series of user requirements. Developers do not need to write functions at the beginning, but only need to present the functions to be implemented in the form of test cases. This is similar to the way in REST to design the system structure through URL. We only need to design reasonable URLs as needed. These URLs do not have to be linked to the specified page or complete some actions, as long as they can intuitively display the user interfaces of the system. Based on these URLs, we can easily design the system structure. From the concept of the REST architecture, everything that can be abstracted into resources can be designated as a URL. What developers need to do is to abstract user requirements into resources, and how to extract images accurately. Because the more accurate the resource abstraction, the better the REST application. This is very different from the Action-based idea in traditional MVC development models. A well-designed URL not only gives developers a clearer understanding of the system structure, but also makes it easier for users to remember and identify resources, because the URL is simple and meaningful. According to the previous design pattern, many URLs are followed by a bunch of parameters, which is inconvenient for users.
Since REST is easy to use, can all Web applications adopt this architecture? The answer is no. We know that, until now, the Model-View-Controller mode is still the most common mode for Web development. Most companies and developers adopt this architecture to develop Web applications, and the way of thinking also stays here. The MVC mode consists of data, views, and controllers. The Controller is triggered by an Event to change the Model and View. With the addition of open-source frameworks such as Webwork and Struts, the MVC development model has become quite mature, and its idea is driven by Action. From the developer's point of view, it is risky to accept a new architecture. There are too many uncertainties. In addition, the new way of thinking of REST is to abstract all user needs as resources, which is difficult to achieve in actual development, because not all user needs can be abstracted as resources, this means that not the entire system structure can be expressed through REST. Therefore, in development, we need to make a choice in REST and MVC Based on the above two points. We think it is better to mix REST and MVC, because it is suitable for most Web application development, developers only need to adopt the REST development mode for user needs that can be easily abstracted as resources, and adopt MVC development for other needs. Here we need to mention the ROR (Ruby on Rails) framework, which is an increasingly popular Web development framework based on the Ruby language. It greatly improves the Web development speed. More importantly, ROR (from version 1.2) is the first Web development framework to introduce REST as the core idea. It provides the best support for REST, it is also the most successful Web development framework for application REST today. In fact, the REST Implementation of ROR is a mix of REST and MVC. developers can use the ROR framework to build Web applications faster and better.
For developers, REST not only contributes to Web development, at the same time, we also learned how to systematically apply software engineering principles to the design and evaluation of a real software.