Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

There is REST for the Weary Developer

We give an example of working with the Representational State Transfer style of software architecture.


advertisement
REST, or Representational State Transfer, is a style of software architecture for accessing information on the Web. The RESTful service refers to web services as resources that use XML over the http protocol. The term REST dates back to 2000, when Roy Fielding used it in his doctoral dissertation.

The W3C recommends using WSDL 2.0 as the language for defining REST web services. To explain REST, let's take an example of purchasing items from a catalog application.

Defining CRUD



First we will define CRUD operations for this service as following. The term CRUD stands for basic database operations Create Read Update and Delete.

From the above table you can see that creating a new item with Id is not supported. When a request for new item is received, Id is created and assigned to the new item. Also, we are not supporting the update and delete operations for the collection of items. Update and delete are supported for the individual items.

Defining XSD

As you know already, REST is based on XML, and the client and server will communicate using XML over http. We can define Item xsd as the following.

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified"> <xs:element name="Item">
<xs:annotation> <xs:documentation> describing root element</xs:documentation>
</xs:annotation> <xs:complexType> <xs:sequence maxOccurs="unbounded"> <xs:element name="Id" type="xs:string"/> <xs:element name="Name" type="xs:string"/> <xs:element name="Url" type="xs:string"/> <xs:element name="Price" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

How CRUD Works

To get a list of items the client can send a GET request to the server:

GET http://myserver:port/catalogapp/RestService/item

In return, the server lists the entire items in the following format confirming to item xsd.

<?xml version="1.0" encoding="UTF-8"?> <Item xsi:noNamespaceSchemaLocation="http://myserver:port/catalogapp/RestService/item/xsd Item.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Id>1</Id>
<Name>Pen</Name> <Url>http://myserver:port/catalogapp/item/1</Url> <Price>3.14</Price> <Id>2</Id> <Name>Book</Name> <Url>http://myserver:port/catalogapp/item/2</Url> <Price>3.00</Price> </Item>

Next, to get the detail of an individual item the client can send request like this:

GET http://myserver:port/catalogapp/RestService/item/1

For this example we have not defined xsd for item details. For now, let's use the same item schema for detail. The server can return information about single item as following.

<?xml version="1.0" encoding="UTF-8"?>
<Item xsi:noNamespaceSchemaLocation="http://myserver:port/catalogapp/RestService/item/xsd Item.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Id>1</Id> <Name>Pen</Name> <Url>http://myserver:port/catalogapp/RestService/item/1</Url> <Price>3.14</Price> </Item>

For creating a new item, the client can send a POST request as seen below. Here we are creating a new item with the name "Cap" and price "2.00."

POST http://myserver:port/catalogapp/RestService/item

<?xml version="1.0" encoding="UTF-8"?>
<Item xsi:noNamespaceSchemaLocation="http://myserver:port/catalogapp/RestService/item/xsd Item.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Name>Pen</Name> <Price>3.14</Price> </Item>

The above post xml for creating a new item does not have Id and Url. The reason is this: catalogapp generates an Id for every new Item. Also, the url is generated when the client executes the send read (GET) operation.

We have defined the "PUT" method for updating an item. But there are servers that do not support the PUT method, and also with HTTP servlets it is more easy to work with POST than PUT. In the operation method detail you can publish the POST method in place of PUT for the update operation. We will continue with the PUT method in this example.

PUT http://myserver:port/catalogapp/RestService/item/1

<?xml version="1.0" encoding="UTF-8"?>
<Item xsi:noNamespaceSchemaLocation="http://myserver:port/catalogapp/RestService/item/xsd Item.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Id>1</Id> <Name>Pen</Name> <Price>3.20</Price> </Item>

The price of the item is updated with the above PUT request. In case you intend to use POST for update operations, you need to decide whether the POST request is create or update operation. The catalogapp can look for tag <Id> and perform other validations to decide whether to create a new item or update an existing item. The Url is generated from catalogapp for GET request, and it is ignored for update.

The client can delete an item by sending a delete request to the server as following.

DELETE http://myserver:port/catalogapp/RestService/item/2

The catalogapp can remove item 2 when it receives this request from the client.

Interface documents

How does the client know what to expect in return when it makes a call for CRUD operations? The answer is the interface document. In this document you can define the CRUD operation mapping, Item.xsd file, and request and response xml. You can have separate xsd for request and response, or response can have text such as "success" in return for the methods other than GET.

Frameworks

There are other frameworks available for RESTful Services. Some of them are listed here.
    * Sun reference implementation for JAX-RS code-named Jersey. (Jersey uses a HTTP web server called Grizzly, and the Servlet Grizzly Servlet)
    * Ruby on Rails
    * Restlet
    * Django
    * Axis2a


   
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap