In spring boot under resources the static and template is not created? - spring-boot

Under resources why the static and template folder is not created automatically?
src/main/resources
Is there any problem if i create the static and the template folder by my own.

Not every application needs these folders, and therefore it doesn't make sense to create them automatically.
Simply create it by yourself.

Related

Reload static content spring boot application

I am using Netbeans and I am developing my first web application using spring boot. I was keeping my HTML, js, CSS in "webapp" folder and then I refactored my project and I put all static content in /resources/static. Since then, I have to rebuild my project every time because the static content isn't reloaded.
Can I easily bypass this problem if I'll use browser-sync plugin for Gulp?
Add the following to src/main/resources/application.properties:
spring.web.resources.static-locations[0]=file:src/main/resources/static/
spring.web.resources.static-locations[1]=classpath:/static/
The "file:" causes the content to be reloaded on refreshing the browser,
see related issue.
Alternatively, the file resource locations can be discovered at runtime and added programmatically.
See also documentation and tutorial.
Note that prior to Spring Boot 2.4, the property was called "spring.resources.static-locations".
Normally the static content is copied to the build directory ( target if you are using maven) by the spring-boot plugin. You can find your files at {build-directory}/classes/static: These are the files that you should modify to reflect changes. You should also copy your changes to resources/static, because whenever you restart spring boot, the files are copied.
if an application.yml file is used for configuration, insert:
spring:
web:
resources:
static-locations[0]: "file:src/main/resources/static/"
static-locations[1]: "classpath:/static/"

Serve static content from folder outside of project

I'm currently developing a Java backend together with JHipster 3 and ran into a problem I don't seem to be able to solve very easily.
I would like to serve static assets – in this case images – from a folder outside of the project in addition to the default front-end generated by JHipster. As of default JHipster seems to serve static assets from one directory out of two depending on environment, as configured in main/java/config/WebConfigurer.java. I would like to point /public/** to a folder in my home catalogue but keep the /** mapping for the Angular front-end.
In general Spring projects you seem to be able to add other sources for static assets by extending WebMvcConfigurerAdapter and override the addResourceHandlers method, but that doesn't seem to have an effect in my case. Adding the #EnableWebMvc annotation breaks the default JHipster mapping for their front-end. If I don't add the annotation I don't even seem to reach handleRequest() in DefaultServletHttpRequestHandler which handles the mapping to the correct servlet.
I can't give any other information on the subject at the moment, but I'm hoping someone with knowledge on JHipster will see this and point me in the right direction.
Thanks in advance, Max.
All app servers have an option to provide additional locations for class path.
For example, Tomcat's property is 'common.loader' in conf/catalina.properties.
You can then use e.g. Spring's ClassPathResource to load a resource manually, or just use a construct like '#Value("classpath:abc.txt") Resource r' to inject something known in advance.

Static content in spring boot....Again

I want to include static content in Spring boot JAR application.
I understood it's recommended to not use /src/main/webapp.
But with IntelliJ, the application run correctly. I tried to change to
/src/main/resources
or
/src/main/public
, but it doesnt work.
/public works, but I dont think its is a good solution.
How I rename static directory and where I have to move please ?
thanks
/src/main/resources is the base folder for all resources. If you place a static|public|resources|META-INF/resources folder inside of it the content should be served (e.g. /src/main/resources/static)

What is the best place to store the application.properties file for a Spring boot application?

I have an application.properties file which I have placed at the same level as the src folder. Things are working correctly. But, is this the standard place to keep this file? What is the best practice?
I place them at src/main/resources. Some would prefer src/main/resources/config. When deploying, I put the customized properties at the same folder where the jar is placed. Again, some would prefer the config sub folder.
Spring Boot picks the files from these locations by default.
Under src folder create another folder named config let's say. Here create another folder named local. Than place your file here. This structure is used when you want to deploy to different servers(i.e. local, test, live).
Then you can create profiles and for example you can configure Maven to choose the proper application.properties file located in one of these folders to create the war and deploy.
So have something live config/local/application.properties and at least config/live/application.properties
UPDATE
What IDE are you using? You need to declare the path as a resource path. See how to do that depending on what you are developing. At build time, the path need to contain some resources, i.e. one or more folders in which the .properties files will be placed.
You can also try the path sugested in another answers src/main/resources (it is more explicit I agree) but the thing is that you can place your resources anywhere but you need to declare the location as a resource location.

Where should I store static resources when I build spring-boot app by gradle?

I've found samples that stores static files in /src/main/webapp/ and /src/main/resources/static/. What's the difference and what's the best place to store static files in spring-boot app?
If your resources are in src/main/webapp that suggests you are building a WAR archive. And for a JAR src/main/resources is more appropriate (eg in /static if it's a boot app). It's up to you though if you want to put stuff in non standard places - you just have to configure the build system to understand what you mean.

Resources