The most important rest principle for Web applications is that the interaction between the client and the server is stateless between requests. Each request from the client to the server must contain the information necessary to understand the request. If the server restarts at any time point between requests, the client will not be notified. In addition, stateless requests can be answered by any available server, which is suitable for cloud computing and other environments. The client can cache data to improve performance.
On the server side, application status and functions can be divided into various resources. A resource is an interesting conceptual entity that is made public to the client. Resource examples include application objects, database records, algorithms, and so on. Each resource uses uri (Universal Resource Identifier) to obtain a unique address. All resources share a unified interface for transmission between the client and server. Standard HTTP methods are used, such as get, put, post, and delete. Hypermedia is the engine of application status. Resources are represented by hyperlink.
Another important rest principle is the hierarchical system, which means that the component cannot understand components other than the middle layer of its interaction with it. By limiting the system knowledge to a single layer, you can limit the complexity of the entire system and promote the independence of the underlying layer.
When the constraints of the rest architecture are used as a whole application, an application that can be extended to a large number of clients will be generated. It also reduces the interaction latency between the client and the server. The unified interface simplifies the entire system architecture and improves the visibility of interaction between subsystems. Rest simplifies the implementation of clients and servers.
Restful implementation: restful Web Services and RPC-style Web Services
After learning about what rest is, let's look at the implementation of restful. Recently, soap-based Web Services built using the RPC style architecture have become the most common method for SOA implementation. The RPC-style Web service client sends an envelope (including method and parameter information) filled with data to the server over HTTP. The server opens the envelope and uses the input parameters to execute the specified method. The result of the method is packaged into an envelope and sent back to the client as a response. The client receives the response and opens the envelope. Each object has its own unique method and only exposes RPC-style Web Services with one URI. Uri represents a single endpoint. It ignores most of HTTP features and only supports the POST method.
Because of its lightweight and direct data transmission over HTTP, the restful method of web services has become the most common alternative. You can use a variety of languages (such as Java programs, Perl, Ruby, Python, PHP, and JavaScript [including Ajax]) to implement the client. Restful web services can generally be accessed through an automatic client or an application representing a user. However, the simplicity of this service allows users to directly interact with it, using their web browser to build a get URL and read the returned content.
In a rest-style Web Service, each resource has an address. Resources are the targets of method calls, and the method list is the same for all resources. These methods are all standard methods, including http get, post, put, delete, and header and options.
In the RPC style architecture, the focus is on methods, while in the REST style architecture, the focus is on resources-the standard method will be used to retrieve and operate information fragments (using the representation form ). The resource representation uses hyperlinks in the representation.
Leonard Richard and Sam Ruby introduced the mixed architecture of the term REST-RPC in their book restful Web Services. REST-RPC Hybrid Web Services transfer data directly over HTTP instead of using envelope packaging methods, parameters, and data, which is similar to rest-style Web Services. However, it does not use standard HTTP methods to operate resources. It stores method information in the URI of an HTTP request. Several well-known web services, such as Yahoo's Flickr API and Del. icio. us API, all use this hybrid architecture.
Restful implementation: Java framework of restful Web Services
There are two Java frameworks to help build restful Web Services. Restlet developed by erome louvel and Dave pawson (see references) is lightweight. It implements concepts such as resources, representations, connectors, and media types for various restful systems, including Web Services. In the restlet framework, both the client and server are components. Components communicate with each other through connectors. The most important class of this framework is the abstract class uniform and its specific subclass restlet. The subclass of this class is a specialized class, such as application, filter, finder, router, and route. These sub-classes can process verification, filtering, security, data conversion, and route incoming requests to corresponding resources. The Resource class generates the client representation.
JSR-311 is Sun Microsystems specification that can define a set of Java APIs for developing restful Web Services. Jersey is a reference implementation for JSR-311.
The JSR-311 provides a set of annotations that can both be used to present Java objects as web resources. This specification assumes that HTTP is the underlying network protocol. It uses annotations to provide clear mappings between Uris and corresponding resource classes, as well as mappings between HTTP methods and Java object methods. API supports a wide range of HTTP entity content types, including HTML, XML, JSON, GIF, and jpg. It also provides the required plug-in functionality to allow additional types to be added through the application using standard methods.
Restful implementation: Build a multi-layer architecture of restful Web Services
Restful Web Services and dynamic web applications are similar in many aspects. Sometimes they provide the same or very similar data and functions, despite the different types of clients. For example, an online e-commerce Classification Website provides users with a browser interface for searching, viewing, and ordering products. It is useful if you also provide Web Services for companies, retailers, and even individuals to automatically order products. Like most dynamic web applications, Web services can benefit from the separation of concerns in a multi-layer architecture. The business logic and data can be shared by the automatic client and GUI client. The only difference lies in the nature of the client and the presentation layer of the intermediate layer. In addition, separating business logic from data access enables database independence and provides plug-ins for various types of data storage.
Figure 1 shows the automated client, including Java and scripts written in various languages, including python, Perl, Ruby, PHP, or command line tools, such as curl. Ajax, Flash, javafx, GWT, blog, and Wiki that run in a browser and run as a restful web service consumer all belong to this column, because they all represent users running in an automated style. The automated Web service client sends an HTTP Response to the resource request handler at the web layer. The client's stateless request contains method information in the header, namely post, get, put, and delete, which maps to the corresponding operations of resources in the resource request handler. Each request contains all required information, including the creden。 used by the resource request handler to process the request.
After receiving the request from the Web service client, the resource request handler requests the service from the business logic layer. Resource request handler identifies all conceptual entities. The system discloses these entities as resources and assigns a unique URI to each resource. However, conceptual entities do not exist in this layer. They exist in the business logic layer. You can use jersey or other frameworks (such as restlet) to implement resource request handler. It should be lightweight and delegate a large amount of duties to the business layer.
Ajax and restful Web services are complementary in nature. They can all use a large number of web technologies and standards, such as HTML, JavaScript, browser objects, XML/JSON, and HTTP. Of course, you do not need to purchase, install, or configure any major components to support interaction between the Ajax front-end and restful Web Services. Restful Web Service provides a simple API for Ajax to process the interaction between resources on the server.
In Figure 1, the Web browser client acts as the front-end of the GUI and uses the HTML generated by browser request handler in the presentation layer to provide the display function. Browser requester handler can use the MVC model (JSF, struts, or spring are examples of Java ). It receives requests from the browser, requests the service from the business logic layer, generates a representation, and responds to the browser. It is displayed in the browser. It not only contains content, but also display attributes, such as HTML and CSS.
Business Rules can be centralized to the business logic layer, which acts as the middle layer for data exchange between the presentation layer and the data access layer. Data is provided to the presentation layer in the form of a domain object or value object. Decoupling browser request handler and resource request handler from the business logic layer facilitates code reuse and implements a flexible and scalable architecture. In addition, since new rest and MVC frameworks can be used in the future, it is easier to implement them without the need to rewrite the business logic layer.
The data access layer provides interaction with the data storage layer, which can be implemented using the DAO design mode or object-relationship ing solution (such as Hibernate, OJB, or ibatis. As an alternative, components in the business layer and data access layer can be implemented as EJB components and get support from EJB containers. This container can facilitate the life cycle of components, manage persistence, transactions, and resource configuration. However, this requires an application server that complies with Java EE (such as JBoss) and may not be able to process tomcat. This layer is used to separate data access code from the business logic for different data storage technologies. The data access layer can also be used as an integration point to connect to other systems and can be used as a client for other Web Services.
The data storage layer includes database systems, LDAP servers, file systems, and enterprise information systems (including legacy systems, transaction processing systems, and Enterprise Resource Planning systems ). With this architecture, you can start to see the power of restful web services, which can flexibly become a unified API for any enterprise data storage, in this way, vertical data is exposed to user-centered web applications and scripts are automatically reported in batches.
What is rest: Conclusion
Rest describes an architecture-style interconnected system (such as Web applications ). A simple, scalable, effective, secure, and reliable architecture is generated when the rest constraints are used as a whole application. Thanks to its simplicity, lightweight, and direct data transmission over HTTP, restful web services have become the most promising alternative to soap-based services. The multi-layer architecture for Web Services and dynamic Web applications can achieve clear separation of reusability, simplicity, scalability, and component responsiveness. Ajax and restful Web services are complementary in nature. Developers can easily use Ajax and restful web services to create rich interfaces.