Home > Java > javaTutorial > How to Serve Static Content with Jersey\'s Universal URL Mapping While Avoiding 404 Errors?

How to Serve Static Content with Jersey\'s Universal URL Mapping While Avoiding 404 Errors?

Patricia Arquette
Release: 2024-10-29 01:45:30
Original
616 people have browsed it

How to Serve Static Content with Jersey's Universal URL Mapping While Avoiding 404 Errors?

Serving Static Content with Jersey's Universal URL Mapping

Jersey's widespread use of the /* URL pattern in its mapping can sometimes cause 404 errors when accessing static resources like HTML files. This issue stems from the fact that the mapping conflicts with the default handling of static content in the web container.

Resolving the Conflict in Jersey 1.x

To serve static content while utilizing the /* mapping in Jersey 1.x, it's recommended to switch from the Jersey servlet to a filter. This can be achieved by updating the web.xml configuration as follows:

<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>
Copy after login

This configuration will allow Jersey to filter requests based on the WebPageContentRegex pattern (/.*html in this case), enabling static resources to be served as intended.

Resolving the Conflict in Jersey 2.x

The solution for Jersey 2.x is similar to that of 1.x, with slight modifications due to changes in property names. The following web.xml configuration should resolve the issue:

<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>
Copy after login

Additionally, the POM file must include the following dependencies:

<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>
Copy after login

The staticContentRegex parameter can be customized to match different file extensions as needed.

As an alternative to using a filter, another approach is to define a versioned path for services, such as "/v1/*", allowing static content to be served without requiring a filter.

The above is the detailed content of How to Serve Static Content with Jersey\'s Universal URL Mapping While Avoiding 404 Errors?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template