Search code examples
javatomcatgradleweb.xmlgradle-tomcat-plugin

Tomcat not serving static files


I'm at the end of my rope on this one. I'm try to get a super simple webapp up and I can't seem to get tomcat to not 404 static files.

  • I'm using the gradle tomcat plugin with tomcat version 7.0.39
  • My html file is at hey-world/src/main/webapp/index.html
  • My web.xml looks like this:

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>HeyWorldApplication</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

So I thought this setup would map localhost:8080/hey-world/static/index.html to the file, but it 404's everytime. Is this a problem with some convention in the gradle tomcat plugin?


Solution

  • The URL-patterns used in web.xml/servlet-mapping is often a little simplistic. I think in your case, the /* pattern for Resteasy will work as a catch-all, so that no other mapping will really matter.

    For debugging, I suggest you remove the Resteasy-servlet altogether, and see if you can serve static files from a custom URL with your mapping. If that works, re-enable Resteasy, but on a different URL-pattern (eg. /rest/*).

    If that works, well, then everything really works fine, it's just that the URL-mapping for /* blocks anything else from working.

    The easiest solution would probably be to server static files as per default (no mapping), and serve rest-stuff from another URL.

    Alternatively use two web apps. One with context root /static, one with context root /.