Home  >  Article  >  Java  >  Why does mapping Jersey\'s URL pattern to /* cause 404 errors for static resources in Jersey 2.0?

Why does mapping Jersey\'s URL pattern to /* cause 404 errors for static resources in Jersey 2.0?

Susan Sarandon
Susan SarandonOriginal
2024-10-27 08:38:02625browse

Why does mapping Jersey's URL pattern to /* cause 404 errors for static resources in Jersey 2.0?

Jersey /* Servlet Mapping Triggering 404 Errors for Static Resources

If Jersey's URL pattern is mapped to /* in Jersey version 2.0, it can lead to 404 errors for all static resources like index.html. Here's the web.xml configuration causing this issue:

<code class="xml"><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></code>

Solution for Jersey 1.x

In Jersey 1.x, you can switch from the Jersey servlet to the filter to enable serving static content:

<code class="xml"><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></code>

Solution for Jersey 2.x

In Jersey 2.x, use the following filter configuration:

<code class="xml"><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></code>

Ensure your POM includes:

<code class="xml"><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></code>

Customize the regular expression in the init-param if serving different static file types (e.g., CSS, JSP).

The above is the detailed content of Why does mapping Jersey\'s URL pattern to /* cause 404 errors for static resources in Jersey 2.0?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn