21

If I map Jersey's url-pattern to /* in the 2.0 release it causes 404 for all static resources (e.g. /index.html). My web.xml has:

<servlet>
  <servlet-name>JerseyApp</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>org.frog.jump.JerseyApp</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>JerseyApp</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

How do I serve static content with same url pattern?

condit
  • 10,852
  • 2
  • 41
  • 60
FUD
  • 5,114
  • 7
  • 39
  • 61

2 Answers2

29

With Jersey 1.x you should be able to serve static content from the same path if you switch from the Jersey servlet to the filter. Drop the servlet XML you've specified and switch it to:

<filter>
  <filter-name>Jersey Filter</filter-name>
  <filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>org.frog.jump.JerseyApp</param-value>
  </init-param>
  <init-param>
    <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
    <param-value>/.*html</param-value>
  </init-param>
</filter> 
<filter-mapping>
  <filter-name>Jersey Filter</filter-name>
  <url-pattern>/*</url-pattern> 
</filter-mapping>

EDIT: In Jersey 2.x you should be able to do the same thing but the names of the properties have been changed. Try something like:

<filter>
  <filter-name>Jersey Filter</filter-name>
  <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
  <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>org.example</param-value>
  </init-param>
  <init-param>
    <param-name>jersey.config.servlet.filter.staticContentRegex</param-name>
    <param-value>/.*html</param-value>
  </init-param>
</filter> 
<filter-mapping>
  <filter-name>Jersey Filter</filter-name>
  <url-pattern>/*</url-pattern> 
</filter-mapping>

And your POM should include:

<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>${jersey2.version}</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet-core</artifactId>
  <version>${jersey2.version}</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

<!-- see. https://eclipse-ee4j.github.io/jersey/ for latest version -->

You'll have to customize the regular expression in the init-param if you want to serve css, jsp, etc.

Another good option is to use a versioned path for your services ("/v1/*") and then static content will work without the filter.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
condit
  • 10,852
  • 2
  • 41
  • 60
  • 2
    If i am not wrong you can no longer do this in 2.x versions. com.sun.jersey.spi.container.servlet.ServletContainer is not part of jersey 2.X i think. – FUD Sep 17 '12 at 08:51
  • Updated the answer to include a 2.x option. The package has changed. – condit Sep 17 '12 at 17:32
  • 1
    Not really related maybe, but i wondered if it would be possible to set these params "programmatically" / Annotation/Properties. Seems like with JEE6 this is not possible. With JEE7 it might be, through Application.getProperites().put(key, value)? – icyerasor Aug 31 '15 at 08:24
  • See [this answer](https://stackoverflow.com/a/29076059/2587435) for another property you can set instead of the one in the above answer. – Paul Samsotha Aug 29 '18 at 04:26
12

You shall add the forwardOn404 filter to address this issue

<filter>
    <filter-name>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</param-value>
    </init-param>
    <!-- pass to next filter if Jersey/App returns 404 -->
    <init-param>
        <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

As done in this example https://github.com/eclipse-ee4j/jersey/blob/master/examples/bookstore-webapp/src/main/webapp/WEB-INF/web.xml

NOTE: Make sure that you are changing the <servlet> configuration to a <filter> configuration. The Jersey ServletContainer is both an HttpServlet and a Filter, so you can configure it as either in your web.xml. In order to use the forwardOn404 property, Jersey needs to be configured as filter.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Dhana Krishnasamy
  • 2,126
  • 17
  • 36