Proper place to put static resources in my Spring Boot project - spring

In my Spring Boot project I cannot get static resources such as css, images and java script file via web browser. Where is proper place to put whole bootstrap dictionary, or how to configure to get desired resources?

Just create static folder in src/main/resources here: https://github.com/bajdekm/MailingHero/tree/master/src/main/resources and place them all there. If you don't create controller with mapping to root resource (something like #RequestMapping("/")), spring boot will serve static content from index.html automatically.
Here is relevant section of Spring Boot documentation.

Related

Spring boot web app - not able to pick the static content

Spring boot web app with angular bundle was working fine earlier
Then I added a gradle dependency which is a library that is built within my organization to achieve some api calls. And after that the view/angular build is not picking up by spring boot application.
My project structure looks like below
web-app\
src\main\java\
WebApplication.java
\resources\public\index.html
\public\main.5214684651aera5146.js
\public\styles.3asd44654e1asd135fsdf.js
\public\other-angular-build-files
application.properties
I haven't added any view controllers/resource handlers by implementing WebMvcConfigurer. Spring boot was able to pick the index.html file as welcome page and I was able to see the page on http://localhost:8080.
But now it is not working, I have gone though below article and tried with different location of view - public, static, resources, META-INF\resources but nothing workout.
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
Can someone help how can I debug this issue or override MVC config so that my spring boot should pick the index.html as the welcome page.
In addition to M. Deinum answer you can try to restrict #ComponentScan only in your app and manually create beans from classes from library

Cannot locate any file inside templates folder from browser

I've made some thymeleaf templates and want to use it with Spring Boot (on Tomcat) and the only location inside a project where I can find a .html file from browser is src/main/resources/static. When I place a file there, I can access it with contextRoot/filename.html. But when I place it inside src/main/resources/templates folder, I cannot access it with neither of this: root/filename.html, root/resources/filename.html. What's the problem here?
Problem was that I didn't have Spring Boot Thymeleaf Starter dependency in the POM. I had plain Thymeleaf dependency not intended for Spring Boot and using it needed more configuration unlike the first one that works out of the box.

Cannot read resource files or refer them in Docker with Spring Boot

I use Spring Boot 2 and Thymeleaf as a frontend framework. And Thymeleaf is referring to templates that are located in src/main/resources/template folder. It is working fine. But when I try to run it in Docker my controller class cannot retrieve templates from resources.
How to make the resources folder available in Docker?

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?
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.
Be wary because Spring Boot 2.4.x disables the default servlet (which impacts on loading of src\main\webapp and anything else at the root of your WAR file. JAR file packaging is disabled by default but there is a configuration option to all that too - see Spring boot: configure it to find the webapp folder.
https://github.com/spring-projects/spring-boot/issues/22915
The full release notes are here: https://spring.io/blog/2020/11/12/spring-boot-2-4-0-available-now
hi if i understand your question
when your are using jar packaging in spring boot
yo will put your resource in META-INF/resources
and you can use this jar package in other project and call resource ,
you can move webapp to meta-inf directory using maven and gradle build

When using Spring + Spring Boot + STS, where in the Spring Project directory is the localhost "home" folder?

I am using Spring/Spring Boot/and Ember. I want to put my index.html file in the "localhost" main folder, but I have no idea how the Spring Tool Suite deploys Spring Boot. What is the normal process for HTML development and accessing a Spring REST server without getting Cross Site Scripting errors?
My folder structure is as follows:
/.settings
/BookingClient
/gradle
/html (this is where my index.html, css, and all Ember related JS stuff is)
/node_modules
/src
/target
I am guessing its somewhere in the /target directory but nothing I have tried has worked.
Spring Boot will serve static resources for you. Simply put them in src/main/resources/static. See the documentation on static content for more information.

Resources