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
 

RESTful Web Services: The Keep-It-Simple Style : Page 2

Discover why RESTful web services are often called web services for developers.


advertisement
REST in the Context—Global Tracking Application
More than anything else, the web has revolutionized how global logistics and supply chain networks operate. Almost all major international carriers such as UPS, FedEx, and DHL offer highly useful and accessible online shipment-tracking services. Updates to shipment status occur nearly in real time as shipments move from location to location. This article expands on these highly valuable, relatively simple features in a small example RESTful application that enables automated web-services-based access and management of information on global shipments. This application will show what you need to build a framework for a REST-compliant application and demonstrate how easy it is to build RESTful web services by using just a few, very rudimentary Java and Java EE APIs.

Note that this application was built following the so-called Hi-REST approach. Hi-REST and Low-REST are informal labels for two sub-styles of REST. Low-REST applications support only POST and GET methods, while Hi-REST applications support all the methods of the HTTP protocol and formally incorporate XML as a data format as well.

Use Cases for the Sample Global Tracking Application
The global tracking system will enable the following:

  • Creating the shipment information—Each shipment has a unique tracking number.
  • Updating the status of the shipment—Shipment is identified by the tracking number.
  • Querying the status of the shipment—Shipment is identified by the tracking number.
  • Deleting the shipment—Shipment is identified by the tracking number.



Named Resources
According to the principles of the REST style, each web-accessible domain object should be represented as a unique URI. In this case, each shipment will be a standalone URI formatted as follows:

http://localhost:8080/GlobalTracking/shipment/1229292919911

Through this URI, one will be able to create, update, query, and delete shipment states.

So, from an implementation perspective, you need to build a platform that can support these two key RESTful design items:

  1. Embedding of the domain object identifiers (in this case, tracking numbers) in the URI as resources
  2. Support for all four HTTP methods: PUT, GET, POST, DELETE

To complete the first requirement of dispatching web requests for URIs constructed in this fashion, the example utilizes the “wildcard” servlet mapping available as an option in the web.xml file:

<servlet-mapping> <servlet-name>Shipment</servlet-name> <url-pattern>/shipment/*</url-pattern> </servlet-mapping>

You also will need to do some parsing in the mapped servlet itself to extract the last element from the passed URI:

String URI = req.getPathInfo(); String shipmentID = URI.substring( URI.lastIndexOf("/") + 1 );

You will use this tracking number to access the shipment and apply the method requested upon it.

Processing of the HTTP Methods
If you're familiar with the Servlets API, you will recall that HttpServlet (the superclass of all servlets) provides a no-functionality implementation for all four methods required by REST (see Class HttpServlet documentation page).

In order to implement the handling logic for HTTP methods, you just need to override each one of these methods in the servlet mapped to handle shipment URIs.

There is, however, a common browser-side issue associated with the implementation of the HTTP methods and their support in form submissions. Most browsers support only POST and GET, and they convert all other methods to GET. To work around this issue, I needed to add a custom hidden form parameter to a test html page (workbench.html):

<input type="hidden" name="method" value="put"/>

I then needed to insert under the HttpServlet an additional superclass that overrides the service method from HttpServlet to parse this parameter and, based on this value, I dispatched the processing to the appropriate do* method that matches HTTP method:

@Override protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String httpMethod = request.getParameter("method"); if ( httpMethod != null && !"".equals(httpMethod.trim() )) { if (httpMethod.equalsIgnoreCase("delete")) { doDelete(request, response); return; } else if (httpMethod.equalsIgnoreCase("put")) { doPut(request, response); return; } } super.service(request, response); }



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap