I can't get my Spring-boot project to serve static content.
I've placed a folder named static
under src/main/resources
. Inside it I have a folder named images
. When I package the app and run it, it can't find the images I have put on that folder.
I've tried to put the static files in public
, resources
and META-INF/resources
but nothing works.
If I jar -tvf app.jar I can see that the files are inside the jar on the right folder:
/static/images/head.png
for example, but calling: http://localhost:8080/images/head.png
, all I get is a 404
Any ideas why spring-boot is not finding this? (I'm using 1.1.4 BTW)
Best Answer
Not to raise the dead after more than a year, but all the previous answers miss some crucial points:
@EnableWebMvc
on your class will disableorg.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
. That's fine if you want complete control but otherwise, it's a problem.There's no need to write any code to add another location for static resources in addition to what is already provided. Looking at
org.springframework.boot.autoconfigure.web.ResourceProperties
from v1.3.0.RELEASE, I see a fieldstaticLocations
that can be configured in theapplication.properties
. Here's a snippet from the source:As mentioned before, the request URL will be resolved relative to these locations. Thus
src/main/resources/static/index.html
will be served when the request URL is/index.html
. The class that is responsible for resolving the path, as of Spring 4.1, isorg.springframework.web.servlet.resource.PathResourceResolver
.Suffix pattern matching is enabled by default which means for a request URL
/index.html
, Spring is going to look for handlers corresponding to/index.html
. This is an issue if the intention is to serve static content. To disable that, extendWebMvcConfigurerAdapter
(but don't use@EnableWebMvc
) and overrideconfigurePathMatch
as shown below:IMHO, the only way to have fewer bugs in your code is not to write code whenever possible. Use what is already provided, even if that takes some research, the return is worth it.
Edit July 2021:
WebMvcConfigurerAdapter
has been deprecated since Spring 5. ImplementWebMvcConfigurer
and annotate with@Configuration
.