Tuesday, May 14, 2013

Using Restlet to build a Java API

I'm working on building a Restful Web Services API for a Java Application that I'm active on. My initial direction for getting started was to look into JAX-RS (Java's API for building restful web services), and then noticing that JAX-RS 1.0 has been out for quite some time, and then noticing that JAX-RS 2.0 was getting approved right before my eyes. Assuming that JAX-RS2 is the successor to JAX-RS1, one might as well stay on the latest edge. JAX-RS 2.0 is now approved by the Java Community Process, so, assuming there are implementations worth using, you can get going now!

Well, I'm stumbling through this "decision matrix" of which implementation to choose, and Stack Overflow only gets you so far. So, for now, I'm doing a technology "Spike", and am working through researching RESTlet to be my horse to build an API upon.


I've recently picked up some Rails development, and I'll note that documentation and getting started started guides are rampant throughout that ecosystem. So, Rails is a nice platform that provides as little friction as possible to someone getting started.

Getting Started with Restlet

I found some Maven settings to add to a new project pom.xml, which allowed me to get started quickly. (Note: I had used this originally, while the Restlet version was pegged at 2.0.0)
Add to your pom.xml to add Restlet 2.1.2 to your maven project.

I then started reading the book "Restlet in Action", and hit a stumbling block with the following code:

public class AccountServerResource extends ServerResource implements AccountResource {
    private int accountId;

    protected void doInit() {
        this.accountId = Integer.parseInt(getRequestAttributes().getAttribute("accountId"));

This .getAttribute("accountId") does not exist in Restlet 2.0.0, so I did a bit of panic, and tried to re-write the code to something analogous I guess, like: 

    protected void doInit() {
        this.accountId = Integer.parseInt(getRequestAttributes().get("accountId").toString());

Not as pretty, but if it works.. But then I'm stuck with not being able to follow along with the book. And I'm assuming that the book was reviewed. So, I take a different approach, maybe I'm using the wrong version of Restlet? I jumped to the Restlet maven repo, and found that the latest stable version, and the version that matches the book is 2.1. So, I changed my pom to use version 2.1.2, previously I was using Restlet 2.0.0.

Thus far, Restlet is so-far-so-good. But, its a lot of learning, when I really just want to get to "done" faster. I'm not sure what getting-started route I would recommend for another developer that I would on-board to this project. i.e. I layed this groundwork, build on top of that, and read some getting started guides, as opposed to starting from scratch with the book.

The application that I'm actively working on is DSpace, the institutional repository, asset management system.