Static content in spring boot....Again - spring-boot

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)

Related

How to deploy Static Vue application with Spring Boot on a Weblogic Server?

I have a Spring boot application deployed on a Weblogic server, and I want to add a Vue/vite frontend to it. I created a Vue project and then built it into static assets. I then placed the contents of the resulting Dist folder (index.html and assets folder) into src/main/resources/static of the Spring Boot project.
Then I generate the war file using Maven, and host that on Weblogic. The Spring Boot section of the app functions as normal, but when I navigate to weblogicurl/myapp/index.html- it is just a blank page rather than the complete frontend I'm expecting. Upon inspection, the blank index.html does have the standard Vue div element with id of "app", so weblogic is detecting and showing the static frontend. However, it is not able to pull any assets to actually display html or use the js - as it is 404ing when index.html tries to pull those built files.
I'm thinking that maybe it's because index.html is trying to pull the js and css assets from /weblogicurl/assets rather than /weblogicurl/myapp/assets but I'm not entirely sure as I'm unfamiliar with the intended structure of a Spring Boot Web app hosted on Weblogic. If that is the case, how would I resolve it? Though maybe it's some other issue, so any input would be greatly appreciated.
I'm assuming that your index.html has src URLs starting with / and that would cause weblogic to look in its default js assets folder. Alternatively, the src might include assets in the URL.
In any cases, you should update these URLs to be relative and correct after build aka remove the starting / and check path from html to js.
Turns out the problem was indeed that it was pulling assets from /weblogicurl rather than /weblogicurl/myapp.
Adding base: '/myapp/' to my vite.config.ts (or vue.config.js for non vite) resolved it.

Spring boot 404 error if the static content is deployed as jar

I have spring boot application and angular js project as two different modules.
The angular js contents ( files inside 'dist' folder) are converted as jar files and included as part of Spring boot application. The folder structure inside the jar is /static/**.
The jar appears inside the lib folder of the spring boot application.
Now when i try to access the application http://localhost:8080, i get 404 error.
It works fine, if i copy the "dist" folder content inside the /resources/static and create spring boot jar file.
Am i missing anything when the static contents are included as jar file. ?
Thanks in advance
I have a similar set up like you but using Angular 4. My Spring Boot version is 1.5.3.RELEASE.
My build is based on Maven with two modules. One for front-end (JAR) and one for backend (WAR), which references first module with Maven dependency mechanics.
Providing static content works out of the box. Since I'm using Spring Security I have to configure access rules for this resources. But this issue should be answered with a 401 and not 404.
Your angular JAR structure seems to be correct. Is your front-end build complete?
The lib folder with the front-end JAR is in my Spring Boot WAR on path /WEB-INF/.
Did you check if there is a bug connected with your used Spring Boot version?
#Moshe Arad #Raj In my understanding from https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot ,
the static contents can be in one of those mentioned locations. However, I myself is facing struggle in serving static content. I was working on spring-boot and angular-cli(6.1.5) based app. For deploy purpose, so that UI works on spring-boot port; I ran ng build. It generates files in src/main/resource/static (changed outputPath in angular.json to resources/static and create an empty dir static in resources) . But spring-boot didn't serve any angular content at 8080. Although index.html has <base href="/">, I thought to be more specific for it to pick/serve .js contents from static dir so I ran :
ng build --base-href / --deploy-url /static/ . This time the generated index.html has /static/runtime.js etc. in . But when accessing localhost:8080/ (right click -> inspect shows 200 response code) still doesn't show any UI content. But hitting localhost:8080/static/runtime.js shows 404 .
I believe there is more to do beyond just adding content to resources/static as probably tomcat is trying to serve it but somehow the content not being displayed. Because I do notice favicon icon (angular icon) on browser tab and the contents of jar file shows generated static files in the path (BOOT-INF/classes/static/index.html etc.)

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/"

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