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

Page 2 of 7

Restlet basics

A basic server with the Restlet API could not possibly be easier, as shown in Listing 1.

Listing 1. A basic server with Restlet

package net.bosatsu.restlet.basic;

import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;

public class SimpleServer {
  public static void main(String[]args) throws Exception {
    Restlet restlet = new Restlet() {
        @Override
        public void handle(Request request, Response response) {
          response.setEntity("Hello, Java RESTafarians!", MediaType.TEXT_PLAIN);
        }
    };

    // Avoid conflicts with other Java containers listening on 8080!
    new Server(Protocol.HTTP, 8182, restlet).start();
  }
}

This application does not do much (except spread good cheer), but it shows off two of Restlet's fundamental principles. First, simple things are simple. More complex activities are certainly possible, but you only worry about them when you need to. REST does not lack the ability to enforce security, constraints, content negotiation, or other important tasks. Those remain largely orthogonal activities, quite distinct from the process of satisfying a RESTful API. You layer the complexity on as needed.

Second, the code in Listing 1 is designed to be portable among container types. Notice that it doesn't specify a container. Restlets are the actual resources that ultimately respond to the requests. There is no distinction between the container handling the request and the information resource responder, as there can be in the servlet model. If you type the code into an IDE and add dependencies on the org.restlet.jar and com.noelios.restlet.jar archives, you can run the application and should see a log message like this:

Dec 7, 2008 11:37:32 PM com.noelios.restlet.http.StreamServerHelper start
INFO: Starting the internal HTTP server

Point a browser to http://localhost:8182, and you should see the friendly greeting.

Behind the scenes, the org.restlet.jar contains all of the major interfaces for this API. The com.noelios.restlet.jar contains a basic implementation of these interfaces and provides a default HTTP handling capability. You will not want to go into production with this HTTP engine, but it is exceptionally convenient for development and testing purposes. You needn't start up a major container to test your RESTful code. Unit and integration testing can be much easier as a result.

The sample in Listing 1 uses a lot of default behavior to create a default Application instance (I'll discuss Application in the next example) and listen for HTTP protocol requests on port 8182. The StreamServerHelper class starts listening on this port and dispatches requests to the Restlet instance as they come in.

Louvel's goal of supporting client-side RESTful Java is also met with ease, as you can see in Listing 2.

Listing 2. A Restlet client

package net.bosatsu.restlet.basic;

import java.io.IOException;

import org.restlet.Client;
import org.restlet.data.Protocol;

public class SimpleClient {
  public static void main(String [] args) throws IOException {
    String uri = (args.length > 0) ? args[0] : "http://localhost:8182" ;
    Client client = new Client(Protocol.HTTP);
    client.get(uri).getEntity().write(System.out);
  }
}

With the SimpleServer still running, launching this new client code with the same JAR dependencies should print out the friendly greeting to the console. Printing the output in this style would obviously not work for binary-oriented MIME types but, again, it is a convenient starting point.

  • Print
  • Feedback

Resources

More from JavaWorld