Jersey /* servlet mapping causes 404 error for static resources(Jersey/* servlet 映射导致静态资源的 404 错误)
问题描述
如果我在 2.0 版本中将 Jersey 的 url 模式映射到/*,它会导致所有静态资源(例如/index.html)出现 404.我的 web.xml 有:
If I map Jersey's url-pattern to /* in the 2.0 release it causes 404 for all static resources (e.g. /index.html). My web.xml has:
<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>
如何提供具有相同 url 模式的静态内容?
How do I serve static content with same url pattern?
推荐答案
使用 Jersey 1.x,如果您从 Jersey servlet 切换到过滤器,您应该能够从同一路径提供静态内容.删除您指定的 servlet XML 并将其切换到:
With Jersey 1.x you should be able to serve static content from the same path if you switch from the Jersey servlet to the filter. Drop the servlet XML you've specified and switch it to:
<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 中,您应该能够做同样的事情,但属性名称已更改.尝试类似:
In Jersey 2.x you should be able to do the same thing but the names of the properties have been changed. Try something like:
<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 应包括:
And your POM should include:
<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 中自定义正则表达式.
You'll have to customize the regular expression in the init-param if you want to serve css, jsp, etc.
另一个不错的选择是为您的服务使用版本化路径(/v1/*"),然后静态内容将在没有过滤器的情况下工作.
Another good option is to use a versioned path for your services ("/v1/*") and then static content will work without the filter.
这篇关于Jersey/* servlet 映射导致静态资源的 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jersey/* servlet 映射导致静态资源的 404 错误
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
