Jersey /* servlet mapping causes 404 error for sta

2019-01-01 11:24发布

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>

How do I serve static content with same url pattern?

标签: java jersey
2条回答
梦醉为红颜
2楼-- · 2019-01-01 11:51

You shall add the forwardOn404 filter to address this issue

<filter>
    <filter-name>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</param-value>
    </init-param>
    <!-- pass to next filter if Jersey/App returns 404 -->
    <init-param>
        <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

As done in this example https://github.com/jersey/jersey/blob/2.17/examples/bookstore-webapp/src/main/webapp/WEB-INF/web.xml

Note: make sure that you are changing the <servlet> configuration to a <filter> configuration. The Jersey ServletContainer is both an HttpServlet and a Filter, so you can configure it as either in your web.xml. In order to use the forwardOn404 property, Jersey needs to be configured as filter.

查看更多
姐姐魅力值爆表
3楼-- · 2019-01-01 11:58

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>

EDIT: 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>

And your POM should include:

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

You'll have to customize the regular expression in the init-param if you want to serve css, jsp, etc.

Another good option is to use a versioned path for your services ("/v1/*") and then static content will work without the filter.

查看更多
登录 后发表回答