By default, Spring serves static resources from the following 4 locations on the classpath:
In a typical maven project structure, any content within
/src/main/resources/ is already on the classpath, you need to create any of these folders there. You can also create all the four folders.
Alternatively, you can customize these locations which we will see later in this post.
Any file placed in these folders will be accessible by its name, you don’t need to mention the folder name.
Below is the maven project structure with these folders.
As you can see, we have created all 4 folders within
/src/main/resources/ and created html files there with names
index4.html, one in each folder.
Now, if you run the application, these files will be accessible by their names. Let’s see the output.
Hit the URL:
Customize the default locations
You can customize the default resource location by a configuration property called “
spring.web.resources.static-locations”. This property takes array of string which means you can provide multiple locations separated by commas. You can provide any location on the classpath or even file system.
Add the following property in
Below is our project structure.
As you can see, we created a folder called ‘custom’ on the classpath and placed
index5.html file in it. Now restart the application and hit the URL.
Note: when we customize the resource locations, the default ones will not work.