2

We are currently using a Jersey JAX-RS implementation to handle our REST requests (server-side Jersey). Our web.xml file is configured so all /rest/* requests are handled by Jersey. That's fine. Our server is currently Tomcat6, and using Java6 ( on Ubuntu 11.04 )

So, ://myserver/rest/customer/ is ok and Jersey calls the class with @Path("/customer") prefix.

Now we also have static content, for example ://myserver/images/... wich loads fine.

Is there a way to do so that we can get rid of the /rest/* prefix and mix it all together?

What we want to achieve : ://myserver/rest/customer/ → Jersey ://myserver/rest/images/ → the native image Directory of the war

Thanks for your help

Sebastien
  • 279
  • 1
  • 10

2 Answers2

2

This is pretty easy to achieve. Just register Jersey ServletContainer in web.xml as a filter (instead of servlet) - see the bottom of this page for an example - and either use ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX or ServletContainer.FEATURE_FILTER_FORWARD_ON_404 init param to make the static content accessible.

Martin Matula
  • 7,969
  • 1
  • 31
  • 35
  • Perfect ! thank you. I agree too with other option, but find this one more suitable. ServletContainer.FEATURE_FILTER_FORWARD_ON_404 is a well hidden option :) – Sebastien Oct 01 '11 at 21:22
0

First, when it comes to servlet mapping, priority goes like this:

  1. Path mapping: /rest/*
  2. Extension mapping: *.png
  3. Default: /

Path mapping + extension mapping: bad idea

So you can't map static resources by their file extension within an existing path mapping (that would be a bad idea anyway, managing all static file extensions in your web.xml).

Path mapping's priority > ext mapping.

Using a filter

Set up an UrlRewriteFilter in your web.xml

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And in your urlrewrite.xml configuration file:

<urlrewrite default-match-type="wildcard">
    <rule>
        <from>/rest/images/**</from>
        <to>/images/$1</to>
    </rule>
</urlrewrite>

Not mapping static resources in /rest

This may not suit your needs, but this is my favorite! Mapping static resources within /rest says "Hey, developer, come GET/POST/PUT/DELETE thoses static resources, it's okay":

  • If you can't GET/PUT/POST/DELETE those resources, then you shouldn't map them in /rest. It gives the wrong impression.
  • If ou actually want to manipulate those resources through a REST webservice, then let Jersey do the heavy lifting and serve those resources (check out the jersey-samples for an example).
Martin Matula
  • 7,969
  • 1
  • 31
  • 35
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • Thanks for your answer. In fact, it's just the contrary we try to achieve. We want to limit Jersey to some special things ( /employees, /customers... ) and let static files ( /images, /dojo, /javascript ) be handled by Tomcat natively. The problem is, for SEO and URL beauty, we'd like to avoid using /rest/employees, /rest/customers. This is this type of mixin we want to achieve. – Sebastien Sep 03 '11 at 09:22