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 6 of 7

Using Restlet's JAX-RS extensions, the Restlet engine binds JAX-RS resources based on the dictates of an Application configuration class. This is a different class from the Restlet Application class discussed above. (This is an unfortunately overloaded name that used to make more sense when JAX-RS called it ApplicationConfig.) The SampleApplication class in Listing 7 indicates that the server should bind the SampleResource class to the @Path annotations that are marked up in its definition.

Listing 7. The SampleApplication class

package net.bosatsu.restlet.jaxrs;

import javax.ws.rs.core.Application;
import java.util.Set;
import java.util.HashSet;

public class SampleApplication extends Application {
  final static Set<Class<?>> classes;

  static {
    classes = new HashSet<Class<?>>();
    classes.add(SampleResource.class);
  }

  public Set<Class<?>> getClasses() {
    return classes;
  }
}

Finally, as shown in Listing 8, a JaxRSApplication is configured with the Application instance to make all of the magic happen.

Listing 8. Using the JaxRsApplication class

package net.bosatsu.restlet.jaxrs;

import org.restlet.Component;
import org.restlet.Server;
import org.restlet.ext.jaxrs.JaxRsApplication;
import org.restlet.data.Protocol;

public class JaxRSServer {
  public static void main(String [] args) throws Exception {
    Component server = new Component();
    Server s = server.getServers().add(Protocol.HTTP, 8182);

    JaxRsApplication app = new JaxRsApplication(server.getContext().createChildContext());
    app.add(new SampleApplication());
    server.getDefaultHost().attach(app);
    server.start();
  }
}

As a valid JSR 311 engine, the JaxRSApplication class iterates over all of the classes advertised from the getClasses() method of the SampleApplication class. It then sets up Routers and whatever else is necessary internally to handle the requests. The rest of the sample looks surprisingly like the previous examples for stock Restlet applications.

In order to make this example run, you need to have dependencies on:

  • com.noelios.restlet.jar
  • org.restlet.jar
  • javax.mail.jar
  • javax.ws.rs.jar
  • org.apache.commons.fileupload.jar
  • org.json.jar
  • org.restlet.ext.jaxrs_1.0.jar

These can all be found in the standard Restlet distribution under the install/lib directory. Note: I had some issues running the examples using JDK 1.6. They worked, but some stream handler made a lot of unpleasant noise when closing up the sockets, so you might want to stick with JDK 1.5.

One of the goals of this initiative is also to make it easy to support content-negotiated forms of the resources being requested. For example, client requests with appropriate "Accept" headers will be honored. The code in Listing 9 uses the Restlet client API to indicate that it will accept only a non-HTML MIME type.

Listing 9. Content negotiation with the Restlet client API

package net.bosatsu.restlet.jaxrs;

import org.restlet.data.*;
import org.restlet.Client;

import java.io.IOException;

public class SampleClient {
  public static void main(String [] args) throws IOException {
    Client client = new Client(Protocol.HTTP);
    Request r = new Request();
    r.setResourceRef("http://127.0.0.1:8182/sample");
    r.setMethod(Method.GET);
    r.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_XML));
    client.handle(r).getEntity().write(System.out);
  }
}

When you execute this code, you should see <message>Hello</message> printed out instead of the HTML you saw earlier. Try changing the request to ask for a text/plain response. There are more elaborate ways of managing the object-to-MIME-type conversion, but this is intended to get you thinking about content-negotiated information resources. There's much more to JAX-RS, but this should give you a taste for what is possible and how it can help reduce some of the drudgery of Java-based REST interfaces.

  • Print
  • Feedback

Resources

More from JavaWorld