Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

java - Jersey /* servlet mapping causes 404 error for static resources

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...