如果我在2.0版本中将Jersey的url-pattern映射到/ *,它将为所有静态资源(例如/index.html)造成404。我的web.xml具有:
/ *
web.xml
<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>
如何使用相同的网址格式投放静态内容?
使用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>2.0-m07-1</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.0-m07-1</version> <type>jar</type> <scope>compile</scope> </dependency>
如果要提供CSS,JSP等服务,则必须在init-param中自定义正则表达式。
另一个不错的选择是为你的服务使用版本路径(“ / v1 / *”),然后静态内容将在没有过滤器的情况下工作。