Reputation: 351
Spring boot is not serving static files placed inside a jar.
I've had a backend app which I decided to add frontend to. Setup tasks to copy static frontend files to src/main/resources/static
. Went through a bunch of answers here on SO, all of them advise the static content (index.html, .js and .css files) should reside under src/main/resources/static
or src/main/resources/public
, which i both tried. I open the built .jar and the static files are there, but starting application using java -jar myApp.jar
and opening localhost:8080
gives me default whitelabel error page. My application works, since i can access the api i have running on it.
Application doesn't have @EnableWebMvc
or any other custom configuration.
If i manually copy/paste same static resources to project's src/main/resources/static
and ran the application using @SpringBootApplication class in IDE - the resources are loaded without problem and index.html opens upon visiting localhost:8080
, so it's only a problem when files are in .jar.
Should the static files be somewhere different when they're in runnable spring boot .jar file?
Spring boot version 2.1.1
Upvotes: 2
Views: 4137
Reputation: 760
I am facing the same problem.
In case it is helpfull, I can manage to serve the static files properly by adding the following configuration :
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
public class StaticFilesConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
... but it overrides the "implicit" auto configuration from spring boot and the rest of my filters etc. doesn't work anymore... => this is because @EnableWebMvc
deactivates spring's default configuration.
[EDIT] In the end, I happened to understand that the jar containing the static files was not included in the built bootJar. You may want to check that. HTH!
Upvotes: 3