Links: Table of Contents | Single HTML

Chapter 6. Deploying a RESTful Web Service

JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and provider classes, and root resource and provider singleton instances. A Web service may extend this class to declare root resource and provider classes. For example,

Example 6.1. Deployment agnostic application model

public class MyApplication extends Application {
                public Set<Class<?>> getClasses() {
                Set<Class<?>> s = new HashSet<Class<?>>();
                s.add(HelloWorldResource.class);
                return s;
                }
                }
            


Alternatively it is possible to reuse one of Jersey's implementations that scans for root resource and provider classes given a classpath or a set of package names. Such classes are automatically added to the set of classes that are returned bygetClasses. For example, the following scans for root resource and provider classes in packages "org.foo.rest", "org.bar.rest" and in any sub-packages of those two:

Example 6.2. Reusing Jersey implementation in your custom application model

public class MyApplication extends PackagesResourceConfig {
                public MyApplication() {
                super("org.foo.rest;org.bar.rest");
                }
                }
            


There are multiple deployment options for the class that implements Application interface in the Servlet 3.0 container. For simple deployments, no web.xml is needed at all. Instead, an @ApplicationPath annotation can be used to annotate the user defined application class and specify the the base resource URI of all application resources:

Example 6.3. Deployment of a JAX-RS application using @ApplicationPath with Servlet 3.0

@ApplicationPath("resources")
                public class MyApplication extends PackagesResourceConfig {
                public MyApplication() {
                super("org.foo.rest;org.bar.rest");
                }
                ...
                }
            


You also need to set maven-war-plugin attribute failOnMissingWebXml to false in pom.xml when building .war without web.xml file using maven:

Example 6.4. Configuration of maven-war-plugin in pom.xml with Servlet 3.0

<plugins>
                ...
                <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
                </plugin>
                ...
                </plugins>


Another deployment option is to declare JAX-RS application details in theweb.xml. This is usually suitable in case of more complex deployments, e.g. when security model needs to be properly defined or when additional initialization parameters have to be passed to Jersey runtime. JAX-RS 1.1 specifies that a fully qualified name of the class that implements Application may be declared in the <servlet-name> element of the JAX-RS application's web.xml. This is supported in a Web container implementing Servlet 3.0 as follows:

Example 6.5. Deployment of a JAX-RS application using web.xml with Servlet 3.0

<web-app>
                <servlet>
                <servlet-name>org.foo.rest.MyApplication</servlet-name>
                </servlet>
                ...
                <servlet-mapping>
                <servlet-name>org.foo.rest.MyApplication</servlet-name>
                <url-pattern>/resources</url-pattern>
                </servlet-mapping>
                ...
                </web-app>


Note that the <servlet-class> element is omitted from the servlet declaration. This is a correct declaration utilizing the Servlet 3.0 extension mechanism. Also note that <servlet-mapping> is used to define the base resource URI.

When running in a Servlet 2.x then instead it is necessary to declare the Jersey specific servlet and pass the Application implementation class name as one of the servlet's init-param entries:

Example 6.6. Deployment of your application using Jersey specific servlet

<web-app>
                <servlet>
                <servlet-name>Jersey Web Application</servlet-name>
                <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
                <init-param>
                <param-name>javax.ws.rs.Application</param-name>
                <param-value>org.foo.rest.MyApplication</param-value>
                </init-param>
                ...
                </servlet>
                ...
                </web-app>


Alternatively a simpler approach is to let Jersey choose the PackagesResourceConfig implementation automatically by declaring the packages as follows:

Example 6.7. Using Jersey specific servlet without an application model instance

<web-app>
                <servlet>
                <servlet-name>Jersey Web Application</servlet-name>
                <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
                <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>org.foo.rest;org.bar.rest</param-value>
                </init-param>
                ...
                </servlet>
                ...
                </web-app>


JAX-RS also provides the ability to obtain a container specific artifact from an Application instance. For example, Jersey supports using Grizzly as follows:

SelectorThread st = RuntimeDelegate.createEndpoint(new MyApplication(), SelectorThread.class);

Jersey also provides Grizzly helper classes to deploy the ServletThread instance at a base URL for in-process deployment.

The Jersey samples provide many examples of Servlet-based and Grizzly-in-process-based deployments.