Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

REST for Java developers: Restlet for the weary

Easy interfaces for building and consuming RESTful Web services in Java

  • Print
  • Feedback

The open source Restlet API reduces the workload involved in building and consuming RESTful APIs in Java. In this second article in the REST for Java developers series, Brian Sletten introduces you to Restlet and walks through an example application in deploying its interfaces into the servlet containers you use today, while also preparing for the systems of the future. Brian also briefly introduces JSR 311: JAX-RS, Sun's effort to integrate RESTful APIs with the Java EE stack.  Level: Intermediate.

Java developers have long been interested in the REST architectural style, but few have yet traveled the distance between the familiar world of objects and the RESTful world of resources. While we may like the fact that RESTful services can be produced or consumed by other languages, we hate having to convert data to and from byte streams. We hate having to think about HTTP when using tools like Apache HTTP Client. We look longingly at objects created by the wsdl2java command, which lets us pass arguments into a SOAP service as easily as any other method call, sweeping the details of invoking a remote service under the rug. And we find the servlet model to be just slightly too disconnected from the resources being produced. Suffice it to say that while we've been able to build RESTful services from scratch, it has not been a pleasant experience.

Political issues have sometimes compounded the technical hurdles. Many managers feel that SOAP-based Web services are the prescribed way of building service-oriented architectures (SOAs) in Java EE. This is changing with the emergence of important activities such as JSR 311, JAX-RS: The Java API for RESTful Web Services, which you'll learn about in this article. If nothing else, this effort is legitimizing RESTful development in the JEE space.

Meanwhile, help has arrived. In elegant fashion, the open source Restlet framework makes it easy to avoid the thorny issues that can arise from using traditional JEE technology to build and consume RESTful services.

Restlet's roots

In an effort to address some of the technical issues involved in doing REST with Java, Jérome Louvel, a French software consultant, sought to create a framework that would provide a more natural fit. He looked first at the NetKernel environment as a starting point. As much as he liked it, it was not a perfect fit for the API-focused framework he sought to make available. The experience did help influence his thinking about the kinds of things that a REST-oriented environment can offer, however. (The next article in this series will explore NetKernel more fully.)

As Louvel worked on his framework, he developed three goals:

  • Simple actions should be simple for basic usage. The defaults should work with minimal effort but also allow for more complex configurations.
  • Code written to this API should be portable across containers. Although servlet-based systems can be moved among containers such as Tomcat, Jetty, and IBM WebSphere, Louvel had a bigger picture in mind. The Servlet specification is tied to HTTP and a blocking I/O model. He wanted his API to be separable from both of these and deployable into the containers in use today. He also wanted them to be usable with little effort in alternate and emerging containers such as Grizzly, AsyncWeb, and the Simple Framework.
  • It should enrich not just the server side of producing RESTful interfaces in Java, but the client side as well. The HttpURLConnection class and Apache HTTP Client are too low-level to integrate cleanly an directly into most applications.

With these goals in mind, he set out to produce the Restlet API. After a few years in flux, the API became stable and a community grew around it. Today, the core API has a vibrant user base, and significant activity is underway for supporting integration with other toolkits and initiatives such as JAX-RS. (Louvel is now on the JAX-RS expert group.)

  • Print
  • Feedback

Resources

More from JavaWorld