Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use `src/main/webapp` to serve static content with Spring Boot?

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?

like image 914
asmaier Avatar asked May 06 '15 12:05

asmaier


People also ask

How does Spring Boot serve static content?

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.

Where do static resources go in spring boot?

Spring Boot will automatically add static web resources located within any of the following directories: /META-INF/resources/ /resources/ /static/

What is static and template folder in spring boot?

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. (


1 Answers

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.

like image 171
Marco Ferrari Avatar answered Oct 18 '22 19:10

Marco Ferrari