Home > Java > javaTutorial > How to Serve Static Resources in Jersey 2.0 Using the Filter Configuration?

How to Serve Static Resources in Jersey 2.0 Using the Filter Configuration?

Susan Sarandon
Release: 2024-10-26 15:11:02
Original
580 people have browsed it

How to Serve Static Resources in Jersey 2.0 Using the Filter Configuration?

Jersey /* Servlet Mapping Causing 404 Errors for Static Resources

Issue:
In Jersey 2.0, mapping the Jersey url-pattern to /* results in 404 errors for all static resources (e.g., /index.html).

Solution:

With Jersey 1.x, static content can be served from the same path by using a filter instead of a servlet. Replace the provided servlet XML with the following:

<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

EDIT for Jersey 2.x:

Use the following XML 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>
Copy after login

Gradle Dependency for Jersey 2.x:

<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

Note:
Customize the staticContentRegex parameter in the filter configuration to serve other file types (e.g., .css, .jsp).

The above is the detailed content of How to Serve Static Resources in Jersey 2.0 Using the Filter Configuration?. 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