I have a "typical" (close to starter project from initializer) Spring Boot 2.4 application with Spring MVC and I want to add caching to static files. For this, I provide my own @Configuration-class which implements WebMvcConfigurer. But whatever I try, I always just receive 404 errors for static ressources once I use my own configuration.
My directory structure for static content
src/main/resources/
|- static/
|- res/
|- css/
|- js/
|- images/
|- favicon.ico
Problem/Question
I think my misunderstanding is in how ResourceHandlerRegistry#addResourceHandler and ResourceHandlerRegistry#addResourceLocations work together.
I tried to do it similar to the default resource resolving (see org.springframework.boot.autoconfigure.web.WebProperties.Resources.CLASSPATH_RESOURCE_LOCATIONS) and ended up with this (not working) code:
@Configuration
public class CacheStaticResourcesConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/favicon.ico", "/res/**", "/images/**")
.addResourceLocations("classpath:/static")
.setCacheControl(CacheControl.maxAge(7, TimeUnit.DAYS)
.noTransform()
.mustRevalidate());
}
}