Http://express.ruanko.com/ruanko-express_37/technologyexchange6.html in recent years, RESTful Web services have gradually become popular, A large number is used to solve the communication problems between heterogeneous systems. Many websites and apps provide APIs that are based on restful web Services, notably Twitter, Google, and project management tools Redmine. This article will provide a brief introduction to restful Web Service, which I hope will help readers.
I. What is RESTful WEB services
The full name of rest is representation state Transfer, which describes the architectural style of designing a Web application, which is a set of architectural constraints and principles that the application or design that satisfies these constraints and principles is restful. and restful Web services, that's what we call RESTful website services. The rest principle is as follows:
Resource is specified by URI
In a Web application, all things should have a unique ID, and the uniform concept of representing IDs is: URIs. URIs make up a global namespace, and using URIs to identify your critical resources means that they have a unique, global ID.
Explicitly using the HTTP method
REST requires the developer to explicitly use the HTTP method, and is used in a manner consistent with the protocol definition. This basic REST design principle establishes a one-to mapping between creating, reading, updating, and deleting (create, read, update, and Delete,crud) operations and HTTP methods. Based on this mapping:
- To create resources on the server, you should use the POST method.
- To retrieve a resource, you should use the GET method.
- To change the state of a resource or update it, you should use the PUT method.
- To delete a resource, you should use the Delete method.
Multiple representations of resources
Provide multiple representations of resources for different needs. The multiple representations described here include XML, JSON, HTML, and so on. That is, the server side needs to provide external resources in various formats for use by different clients. For example, mobile apps can use XML or JSON and server-side communication, while browsers can understand HTML.
No status
Requests to the server should be stateless, and a complete, independent request does not require the server to retrieve any type of application context or State when processing the request. Stateless constraints make changes to the server invisible to the client, because in two successive requests, the client is not dependent on the same server. A client receives a document from a server that contains a link, and when it does some processing, the server goes down, maybe the hard drive is broken and it is repaired, maybe the software needs an upgrade reboot--If the client accesses a link received from this server, it will not notice that the server in the background has changed.
Ii. Comparison of RESTful Web services with SOAP-based Web services
In the previous article, readers should have a general understanding of restful web Services and a little bit more interest. But the reader may ask, SOAP-based Web services is also a common solution to the problem of communication between heterogeneous systems, so what are the advantages of RESTful Web services versus SOAP-based Web services? Or why are we starting to learn about restful Web services, using SOAP-based Web services that have been popular for a long time?
RESTful Web Services interfaces are easier to use
While RESTful Web Services uses standard HTTP methods (Get/put/post/delete) to abstract the service capabilities of all Web systems, the difference is that SOAP applications abstract Web services by defining their own personalized interface methods. In contrast, a RESTful Web services interface is simpler.
The advantages of RESTful WEB services using standard HTTP methods, in large terms: standardized HTTP operation methods, combined with other standardized techniques, such as uri,html,xml, will greatly improve the interoperability between systems and systems. Especially in Web applications, the abstraction that RESTful Web services conveys is more natural than the way the web itself works.
At the same time, rrestful WEB services, implemented using the standard HTTP method, also brings some advantages of the HTTP method itself:
- Non-state sex
The HTTP protocol is essentially a stateless protocol, where HTTP requests from clients can be isolated from each other, and there is no mutual state dependency. HTTP-based ROA, which implements stateless service request processing logic in a very natural way. For distributed applications, any given two service requests request 1 with request 2, because they do not have a state dependency between them, they do not need to collaborate with each other, the result is: request 1 and request 2 can be executed on any server, Such an application can easily support load balancing (load-balance) on the server side.
- Security operation and power-reference equality characteristics
HTTP request should essentially be a secure call, that is: call does not have any side effects, does not cause the server side state changes. For a server, the client makes N-times a GET, haed call to a URI, and its state is the same as not making a call, and no change will occur.
The put, delte invocation of HTTP has a power-reference equality feature, that is, the client makes N-times a PUT, Delte call to a URI, and the effect is the same as the call to do it once. The GET and HEAD methods of HTTP also have power-reference equality attributes.
HTTP these standard methods guarantee in principle that your distributed system has these features to help build a more robust distributed system.
RESTful WEB Services makes caching easier
As we all know, for network-based distributed applications, network transmission is an important factor affecting application performance. How to use caching to save the overhead of network transport is an issue that every developer who constructs a distributed network application must consider.
The HTTP protocol conditional HTTP GET request (Conditional get) is designed to save the overhead of network transport between the client and the server, which also provides the possibility for the client to implement the Cache mechanism, including any proxy between the client and the server. The HTTP protocol implements a conditional get request through the HTTP HEADER domain: If-modified-since/last-modified,if-none-match/etag.
REST applications can fully exploit the HTTP Protocol's ability to support caching. When the client first sends an HTTP GET request to the server for content, the content may be cached by the cache server. The next time the client requests the same resource, the cache can give a direct response without requiring the remote server to obtain it. And all of this is transparent to the client.
Iii. Summary
In everyday applications, we have a number of occasions where we can use restful web Services, including interactions between Web systems, mobile clients and Web server-side communications. RESTful Web Services can be better understood only through more restful practices in daily work.
This article is just a rough introduction to restful WEB services, hoping to help readers get a glimpse of the field, and if more in-depth understanding is needed, readers will need to read more information from other aspects and apply it extensively in practice.
On RESTful Web Services