Adding external static files (css, js, png ...) in spring for boot
Background
I have a spring boot application where the file is logo.png
added to static
the resource file folder , which is eventually embedded in a jar file that is used to execute.
This jar app needs to be run in multiple instances for different clients. So, I created an external file application.properties
that differentiates the settings for each user.
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Problem
But the problem is I need to change the logo of each instance of my application. I cannot insert customer logos into my app bank. Rather, I need to keep it external like my application.properties.
For now, what I have done is check the file logo.png
in the same jar
execution folder and if excist, read the file, get the base64 data and show it in the tag img
.
But I want this to be done correctly as static content. I need static content to be externalized. so I can let each client have a specific jar instance working with different static resource content
For example. I need to store external static files like below and access urls in my views href
or src
html tags attributes.
Summary
Required folder structure
+ runtime
- myapp-0.1.0.jar
- application.properties
+ static
- logo.png
Must have access to
<img th:src="@{/logo.png}" />
source to share
You can use resource handlers to serve external files - for example,
@Component
class WebConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/ext/**").addResourceLocations("file:///yourPath/static/");
}
}
source to share