Spring Boot project with static content generates 404 when running jar
I am banging my head against the wall trying to figure out how to do this with gradle. Any tips?
EDIT: I got it to work by adding this to my build.gradle:
// Copy resources into the jar as static content, where Spring expects it.
jar.into('static') {
from('src/main/webapp')
}
It turns out that whilst Spring Boot is being clever at adding the various resource directories to the classpath, Maven is not and it's up to you to deal with that part. By default, only src/main/resources
will be included in your JAR. If you create a folder called /static
in the root of your project (as implied by the blog post) then it will work fine whilst using the spring-boot:run Maven goal but not once you create a JAR.
The easiest solution is to create your /static
folder inside /src/main/resources
and then Maven will include it in the JAR. Alternative you can add additional resource locations to your Maven project:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>static</directory>
<targetPath>static</targetPath>
</resource>
</resources>
I hope this is useful to someone, it's kind of obvious when you step back and look at how Maven works but it might stump a few people using Spring Boot as it's designed to be pretty much configuration free.