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>
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>
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>
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!