使用 Jersey 1.x,如果您从 Jersey servlet 切换到过滤器,您应该能够从同一路径提供静态内容。删除您指定的 servlet 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>
编辑:在 Jersey 2.x 中,您应该能够做同样的事情,但属性的名称已更改。尝试类似:
<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>
你的 POM 应该包括:
<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 -->
如果要提供 css、jsp 等服务,则必须在 init-param 中自定义正则表达式。
另一个不错的选择是为您的服务使用版本化路径(“/v1/*”),然后静态内容将在没有过滤器的情况下工作。