RESTful systems should have a uniform interface. HTTP 1.1 provides a set of methods,called verbs,for this purpose. Among these the more important verbs are:
GET
?is probably the most popular method on the Web. It is used to fetch a resource.
HEAD
?returns only the response headers with an empty body. This method can be used in a scenario when you do not need the entire representation of the resource. For example,?HEAD
can be used to quickly check whether a resource exists on the server or not.
The method?OPTIONS
?is used to get a list of allowed operations on the resource. For example consider the request:
<pre class="brush: text; html: collapse;">OPTIONS http://MyService/Persons/1 HTTP/1.1
HOST: MyService
The service after authorizing and authenticating the request can return something like:
<pre class="brush: text; html: collapse;">200 OK
Allow: HEAD,GET,PUT
The second line contains the list of operations that are allowed for this client.
You should use these methods only for the purpose for which they are intended. For instance,never use?GET
?to create or delete a resource on the server. If you do,it will confuse your clients and they may end up performing unintended operations. To illustrate,this let's consider this request:
<pre class="brush: text; html: collapse;">GET http://MyService/DeletePersons/1 HTTP/1.1
HOST: MyService
By HTTP 1.1 specification,a?GET
?request is supposed to fetch resources from the server. But it is very easy to implement your service such that this request actually deletes a?Person
. This request may work perfectly,but this is not a RESTful design. Instead,use the?DELETE
?method to delete a resource like this:
<pre class="brush: text; html: collapse;">DELETE http://MyService/Persons/1 HTTP/1.1
HOST: MyService
REST recommends a uniform interface and HTTP provides you that uniform interface. However,it is up to service architects and developers to keep it uniform.
Difference between PUT and POST
The short descriptions of these two methods I provided above are almost the same. These two methods confuse a lot of developers. So let's discuss these separately.
The key difference between?PUT
?and?POST
?is that?PUT
?is idempotent while?POST
?is not. No matter how many times you send a?PUT
?request,the results will be same.?POST
?is not an idempotent method. Making a?POST
?multiple times may result in multiple resources getting created on the server.
Another difference is that,with?PUT
,you must always specify the complete URI of the resource. This implies that the client should be able to construct the URI of a resource even if it does not yet exist on the server. This is possible when it is the client's job to choose a unique name or ID for the resource,just like creating a user on the server requires the client to choose a user ID. If a client is not able to guess the complete URI of the resource,then you have no option but to use?POST
.
PUT
?request will not modify or create more than one resource no matter how many times it is fired (if the URI is same). There is no difference between?PUT
?and?POST
?if the resource already exists,both update the existing resource. The third request?(POST http://MyService/Persons/
) will create a resource each time it is fired. A lot of developers think that REST does not allow?POST
?to be used for update operation; however,REST imposes no such restrictions.
Statelessness
A RESTful service is stateless and does not maintain the application state for any client. A request cannot be dependent on a past request and a service treats each request independently. HTTP is a stateless protocol by design and you need to do something extra to implement a stateful service using HTTP. But it is really easy to implement stateful services with current technologies. We need a clear understanding of a stateless and stateful design so that we can avoid misinterpretation.
A stateless design looks like so:
Request1:?GET http://MyService/Persons/1 HTTP/1.1
Request2:?GET http://MyService/Persons/2 HTTP/1.1
Each of these requests can be treated separately.
A stateful design,on the other hand,looks like so:
Request1:?GET http://MyService/Persons/1 HTTP/1.1
Request2:?GET http://MyService/NextPerson HTTP/1.1
To process the second request,the server needs to remember the last?PersonID
?that the client fetched. In other words,the server needs to remember the current state — otherwise Request2 cannot be processed. Design your service in a way that a request never refers to a previous request. Stateless services are easier to host,easy to maintain,and more scalable. Plus,such services can provide better response time to requests,as it is much easier to load balance them.
Links Between Resources
A resource representation can contain links to other resources like an HTML page contains links to other pages. The representations returned by the service should drive the process flow as in case of a website. When you visit any website,you are presented with an index page. You click one of the links and move to another page and so on. Here,the representation is in the HTML documents and the user is driven through the website by these HTML documents themselves. The user does not need a map before coming to a website. A service can be (and should be) designed in the same manner.
Let's consider the case in which a client requests one resource that contains multiple other resources. Instead of dumping all these resources,you can list the resources and provide links to them. Links help keep the representations small in size.
For an example,if multiple?Person
s can be part of a?Club
,then a?Club
?can be represented inMyService
?as in Listing Six:
Listing Six: A Club with links to Persons.
Caching
Caching is the concept of storing the generated results and using the stored results instead of generating them repeatedly if the same request arrives in the near future. This can be done on the client,the server,or on any other component between them,such as a proxy server. Caching is a great way of enhancing the service performance,but if not managed properly,it can result in client being served stale results.
Caching can be controlled using these HTTP headers:
Cache-Control
header to check if the cached results are still valid or not. The most common directives forCache-Control
?header are:
Documenting a RESTful Service
RESTful services do not necessarily require a document to help clients discover them. Due to URIs,links,and a uniform interface,it is extremely simple to discover RESTful services at runtime. A client can simply know the base address of the service and from there it can discover the service on its own by traversing through the resources using links. The methodOPTION
?can be used effectively in the process of discovering a service.
This does not mean that RESTful services require no documentation at all. There is no excuse for not documenting your service. You should document every resource and URI for client developers. You can use any format for structuring your document,but it should contain enough information about resources,URIs,Available Methods,and any other information required for accessing your service. The Table below is a sample documentation of?MyService
. This is a simple and short document that contains all the aspects of?MyService
?and should be sufficient for developing a client.
Service Name: MyService
Address: http://MyService/
Conclusion
REST is a great way of developing lightweight Web services that are easy to implement,maintain,and discover. HTTP provides an excellent interface to implement RESTful services with features like a uniform interface and caching. However,it is up to developers to implement and utilize these features correctly. If we get the basics right,a RESTful service can be easily implemented using any of the existing technologies such as Python,.NET,or Java. I hope this article provides enough information for you to start developing your own RESTful services.
reference from:
http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069?pgno=1
http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069?pgno=2
http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069?pgno=3
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!