Jersey 2 on JBoss 7

October 27, 2014

I received an email asking how I deployed Jersey 2 on JBoss 7 (specifically 7.1.1) while still being able to access Jersey resources (see my Stackoverflow post here). The solution that worked for me was to add a jboss-deployment-structure.xml in my app’s WEB-INF directory and manually register Jersey resources rather than rely on automatic package scanning:



Here’s the jboss-deployment-structure.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <!-- Exclude RestEasy conflict -->
            <module name="javaee.api" />
            <module name="javax.ws.rs.api"/>
            <module name="org.jboss.resteasy.resteasy-jaxrs" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

Here’s a simplified web.xml:

<web-app>
    <display-name>WebApp</display-name>

    <servlet>
        <servlet-name>WebApp</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- This doesn't work in JBoss7 -->
        <!--<init-param>-->
            <!--<param-name>jersey.config.server.provider.packages</param-name>-->
            <!--<param-value>io.minh.resources</param-value>-->
        <!--</init-param>-->
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>io.minh.ApplicationConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>WebApp</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>


    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.providers</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.resources</param-name>
        <param-value>false</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>


Here’s an implementation of the javax.ws.rs.Application / ResourceConfig referenced in the web.xml above, where each Jersey resource needs to be manually registered (there’s probably a way to make this work automatically, but as shown below the automatic package scanning wasn’t working in JBoss…):

package io.minh;
import org.glassfish.jersey.server.ResourceConfig;

public class ApplicationConfig extends ResourceConfig {

	public ApplicationConfig() {
		// package scanning doesn't work in Jboss for some reason
		// packages( true, "io.minh" );
		register(SomeResource.class);
	}

}


Lastly, here’s the Jersey library I used in my Maven pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.5</version>
</dependency>