The documentation of Spring Boot states:
Do not use the src/main/webapp directory if your application will be packaged as a jar.
But surprisingly the Spring Boot Sample for static web files is using the /src/main/webapp
directory. And also JHipster is using the webapp
folder.
So I'm confused. Is the warning in the documentation of Spring Boot outdated? Is it now considered good practice to use src/main/webapp
to serve static files with Spring Boot jar applications? And if not, what is the recommended practice now when using Spring Boot in a Maven setup?
Spring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.
Spring Boot will automatically add static web resources located within any of the following directories: /META-INF/resources/ /resources/ /static/
templates folder is a place where you put all the thymeleaf templates. It is a default directory (by default spring will look inside for any templates). static folder is used for serving web static content, all the css, js, html etc. (
From Spring Boot Reference Documentation (emphasis mine):
By default Spring Boot will serve static content from a directory called
/static
(or/public
or/resources
or/META-INF/resources
) in the classpath or from the root of the ServletContext. It uses the ResourceHttpRequestHandler from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter and overriding the addResourceHandlers method.
In some of my (Maven) projects I am currently using src/main/resources/static
because it's considered part of the classpath by default and IDEs (like Eclipse) tend to like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With