Deploying a JAX-RS application

Applications utilizing the IBM JAX-RS runtime can be deployed into any web container. The following steps walk through the deployment process.

Building the Application sub-class

Before you can package your JAX-RS application, you have to identify which classes will represent the root resources available in your RESTful service. Root resources are the ones that will be accessible at the root level of your application. In other words, they are the resources that will appear just below the context root. For our AddressBook sample, there is only one root resource, "/address".

Once you have identified your root resources, they need to be surfaced to the runtime via the javax.ws.rs.core.Application configuration mechanism. JAX-RS defines this class as a means for aggregating the JAX-RS related artifacts that exist in an application. Classes included in the Application configuration will be treated as root-level resources in the JAX-RS service. As defined by the specification, this is the only portable means of configuring a JAX-RS application.

To create the config class, create a new class in your environment that extends javax.ws.rs.core.Application. This subclass will provide a set of root resources and providers (if any exist) to the runtime. The code sample below demonstrates a sample Application class. This code is based on the AddressBook sample that was developed here.

package com.test;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

public class AddressBookApplication extends Application {

    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(AddressBook.class);
        return classes;
    }
    
}

Packaging the application for web container deployment

JAX-RS applications can be deployed in a JavaEE 5 web container as a Web Archive Module(WAR). There are two ways to package applicatons for use in a web container environment. You can package the application using the provided ant script or package the application manually. Although we recommend using the automated build script, we understand that is not possible in all circumstances.

Packaging using the build script

Instructions for using the provided build script can be found here

Packaging manually

The steps below describe what is needed to package the WAR module manually.

  1. Construct a Web Archive Module(WAR) that contains user-supplied JAX-RS resources along with the libraries provided in the IBM JAX-RS distribution. The libraries should be packaged in the WEB-INF/lib directory of the WAR, and the JAX-RS resource classes should be packaged in the WEB-INF/classes directory.
  2. Configure the web.xml of the WAR to provide the javax.ws.rs.core.Application subclass that is contained within the user application (this will be automatically produced when using the supplied build.xml):

    <servlet>
    	<servlet-name>JAX-RS Servlet</servlet-name>
    	<servlet-class>com.ibm.ws.jaxrs.web.RESTServlet</servlet-class>
    	<init-params>
    		<param-name>javax.ws.rs.Application</param-name>
    		<param-value>com.test.AddressBookApplication</param-value>
    	</init-params>
    </servlet>
    <servlet-mapping>
    	<servlet-name>JAX-RS Servlet</servlet-name>
    	<url-pattern>/*</url-pattern>
    </servlet-mapping>

  3. Deploy the configured WAR into a web container. The root level JAX-RS resources should be available at URL patterns of the form:

    http://<host>:<port>/<context_root>/(@Path.value)

    Consider the following resource:

    @Path(value = "/addresses")
    public class AddressBook {
     
     ...
    
    }

    If this were deployed in a WAR whose context root was "/rest", the URL to the resource would be:

    http://<host>:<port>/rest/addresses