I am attempting to deploy Jersey 1.13 and Struts 2 inside the same WAR file on Glassfish.
I am using a very old version of Glassfish and unfortunately am stuck on this version (Version = Sun Java System Application Server 9.1_02)
The Struts 2 configuration is as per the documentation. THe problem is the filter will intercept all urls and attempt to handle them via the Struts 2 configuration. My question is how do you deploy Jersey (or any servlet) inside the same war as a Struts 2 application?
Below is the web.xml file that I am using.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>JerseyServletAdaptor</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>com.example.portal.api</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServletAdaptor</servlet-name>
<url-pattern>/helloworld</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesConatiner.DEFINITIONS_CONFIG</param-name>
<param-value>tiles.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>com.example.portal.web.actions.WLSessionFilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/jsp/index.html</welcome-file>
</welcome-file-list>
</web-app>
I was able to resolve this as follows:
In web.xml
In struts.xml I added this under the element
The URL is still sent to the struts framework first but is ignored due to the above excludePattern. This then allows my servlet mapping to handle the URL and pass it into the Jersey framework.
This approach does mean you have to prefix your Jersey URLs with api (or what ever you define) e.g.
http://<domain-name>/api/resource/idThis works until you add security into the mix. The problem comes from wanting to use FORM based authentication for Struts and Basic Authentication for Jersey. Unfortunately web.xml only allows you to define a single login-config for an application which meant I was stuck using either FORM based authentication or Basic Authentication for both Struts and Jersey.
Given this I decided to deploy the Jersey application in it’s own WAR which means I didn’t end up using the above solution in the end. The good news is that if you don’t want to use different authentication methods (or any authentication) the above approach does work for deploying Jersey and Struts 2 in the same WAR.