RESTful Web Services
http://java.sun.com/developer/technicalArticles/WebServices/restful/The HTTP methods such as GET
and POST
are the verbs that the developer can use to describe the necessary create, read, update, and delete (CRUD) actions to be performed. Some may see an analogy to operations in SQL, which also relies on a few common verbs, as shown in Table 1. However, the REST style and HTTP protocol are mutually exclusive, and REST does not require HTTP.
Table 1: Relationships Between SQL and HTTP Verbs |
Action | SQL | HTTP |
---|---|---|
![]() | ||
Create | Insert | PUT |
Read | Select | GET |
Update | Update | POST |
Delete | Delete | DELETE |
![]() |
![]() |
Architects and developers need to decide when this particular style is an appropriate choice for their applications. A RESTFul design may be appropriate when
The web services are completely stateless. A good test is to consider whether the interaction can survive a restart of the server.
A caching infrastructure can be leveraged for performance. If the data that the web service returns is not dynamically generated and can be cached, then the caching infrastructure that web servers and other intermediaries inherently provide can be leveraged to improve performance. However, the developer must take care because such caches are limited to the HTTP
GET
method for most servers.The service producer and service consumer have a mutual understanding of the context and content being passed along. Because there is no formal way to describe the web services interface, both parties must agree out of band on the schemas that describe the data being exchanged and on ways to process it meaningfully. In the real world, most commercial applications that expose services as RESTful implementations also distribute so-called value-added toolkits that describe the interfaces to developers in popular programming languages.
Bandwidth is particularly important and needs to be limited. REST is particularly useful for limited-profile devices such as PDAs and mobile phones, for which the overhead of headers and additional layers of SOAP elements on the XML payload must be restricted.
Web service delivery or aggregation into existing web sites can be enabled easily with a RESTful style. Developers can use technologies such as Asynchronous JavaScript with XML ((Ajax)) and toolkits such as Direct Web Remoting (DWR) to consume the services in their web applications. Rather than starting from scratch, services can be exposed with XML and consumed by HTML pages without significantly refactoring the existing web site architecture. Existing developers will be more productive because they are adding to something they are already familiar with, rather than having to start from scratch with new technology.
With RESTFul web services, there is a natural mapping between the HTTP methods and most CRUD-like business operations that many services expose. Though there are no hard and fast rules, the following general guidelines are applicable for most cases:
GET
is used to retrieve data or perform a query on a resource. The data returned from the web service is a representation of the requested resource.POST
is used to create a new resource. The web service may respond with data or status indicating success or failure.PUT
is used to update existing resources or data.DELETE
is used to remove a resource or data.
No comments:
Post a Comment