0

I have a GWT-Spring project and I have also two modules which are used as a libraries in the web project, everything works fine, but I'm trying to put some static resources on one of those modules (JAR) but I'm not seeing my resources when the application is deployed, I'm just getting a 404 when I try to get them.

I'm using Servlet 3.0 in the web.xml and I put this line in my application-config:

<mvc:resources mapping="/resources/**"  location="classpath:/META-INF/resources" ></mvc:resources>

Also I put all my resources under the folder META-INF/resources inside the JAR.

Running the project with Jetty (IntelliJ) if I go to: http://localhost:8888/path/resources I can see the whole list of those folders (All folders I put on the META-INF/resources JAR and the resources on the web project, but about the JAR I can see only the folders, not the files on them!.)

And if I run the project with tomcat I can see only the resources on the web project but anything from the JAR.

Any ideas?

calbertts
  • 1,513
  • 2
  • 15
  • 34

2 Answers2

1

The serving of /META-INF/resources/ from jars found in /WEB-INF/lib/*.jar is a feature of the Servlet 3.0 spec.

As such, the internal implementation of Jetty (namely its DefaultServlet) is responsible for serving this content back on requests for this content.

On Jetty this is accomplished by unpacking the /META-INF/resources/ contents into the WebApp working directory to be served as normal files from disk.

However, you are using Spring MVC, and your configuration appears to be attempting to circumvent this facility of the Container. Don't let Spring MVC handle or serve those resources, let it flow out of Spring and let the Web Container itself serve those resources.

Besides, Jetty's implementation can serve these (and really any kind of) static resource way way better than any generic Servlet can (it uses internal features of Jetty to accomplish this).

Example:

Lets say you had foo.war, with a /WEB-INF/lib/bar.jar that contained a single resource /META-INF/resources/js/main.js.

Assuming you had a Jetty Server with a connector on localhost:8080, with default webapp Deployment, resulting in a context path of /foo for your foo.war, then this resource could be accessed with a request to http://localhost:8080/foo/js/main.js

Created an example project demonstrating this at:

github.com/jetty-project/jetty-example-metainf-resources

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Actually, I'm not using Spring Controllers, only GWT Services that line was a desperate way to find out a solution, I tried without that line trying just to let to the Web Container work as it should work, but it didn't. – calbertts Sep 25 '15 at 20:21
  • There might be some confusion on how you access those resources. There's no `/resources/` path element (that you reference in your question) to worry about when using this concept. – Joakim Erdfelt Sep 25 '15 at 20:27
  • 1
    Went ahead and created an example project for you. – Joakim Erdfelt Sep 25 '15 at 20:48
  • Does any difference to have the META-INF folder under src/main/java? because I'm seeing that you have that folder under src/main/resources – calbertts Sep 25 '15 at 21:01
  • Per maven convention (for `jar`) the `/src/main/java/` path is *only* for `*.java` source files. All other content destined for the jar file needs to be in `/src/main/resources/`, also by convention. – Joakim Erdfelt Sep 25 '15 at 21:34
  • OK, I've already moved everything to resources folder and it still doesn't work, I've checked the JAR content and it has all the resources I need in the same way that you project has, but for some reason when the web project is started only the folder structure is copied to the deploy folder but the content (CSS, js, etc...) keeps empty. – calbertts Sep 25 '15 at 21:39
  • As a side note I'm not using Maven but Gradle. – calbertts Sep 25 '15 at 21:44
  • Your talking to [a Maven developer](http://maven.apache.org/team-list.html#joakime), sorry, don't use Gradle, can't help you. – Joakim Erdfelt Sep 25 '15 at 21:48
0

I found the issue.

This was just a security problem, if the URLs are protected any resource will be available if there's no a session started.

Additionally the resources inside of the WEB-INF/lib/*.jar!/META-INF/resources aren't published under another "resources" folder but published on the root directly.

Thanks.

calbertts
  • 1,513
  • 2
  • 15
  • 34