devxlogo

There is REST for the Weary Developer

There is REST for the Weary Developer

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.

 attributeFormDefault="unqualified">	
describing root element

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.

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">	1 
Pen http://myserver:port/catalogapp/item/1 3.14 2 Book http://myserver:port/catalogapp/item/2 3.00

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.


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1 Pen http://myserver:port/catalogapp/RestService/item/1 3.14

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


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> Pen 3.14

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


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 1 Pen 3.20

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 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
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist