Master Spring Boot Static Resources: Paths, Custom Locations, and Cache Busting
This guide explains how Spring Boot 2.7.10 serves static files, how to customize resource locations and URL patterns, configure WebJars, enable cache‑busting, and reveals the underlying MVC components that handle static resource requests.
Environment: Spring Boot 2.7.10
By default, Spring Boot serves static content from classpath locations /static , /public , /resources , or /META-INF/resources (or the servlet‑context root) using ResourceHttpRequestHandler . You can modify this behavior by adding a WebMvcConfigurer and overriding addResourceHandlers .
Static resources are mapped to /** by default, but the pattern can be changed with the spring.mvc.static-path-pattern property. For example, relocating all resources to /resources/** is possible.
Default static resource locations
<code>spring:
web:
resources:
static-locations:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/</code>Directory structure illustration:
Default access URL: http://localhost:8080/xxx.yy
Modify access path
<code>spring:
mvc:
static-path-pattern: /res/**</code>After modification, the URL becomes http://localhost:8080/res/xxx.yy .
Note: In older Spring Boot versions the property is spring.resources.static-locations .
Add custom static resource locations
<code>spring:
web:
resources:
static-locations:
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
- file:///D:/images/</code>The file:///D:/images/ entry points to a custom file‑system directory (three slashes are required).
Programmatic configuration
<code>@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("file:///d:/images/");
registry.addResourceHandler("/h5/**")
.addResourceLocations("file:///d:/h5/");
}
}</code>These handlers expose the two file‑system directories at /static/** and /h5/** . Access via http://localhost:8080/static/xxx.yy and http://localhost:8080/h5/xxx .
WebJars static resources
Resources under /webjars/** are served from JAR files that contain WebJars‑packaged assets.
If your application is packaged as a JAR, do not use src/main/webapp ; that directory is only for WAR packaging.
Spring Boot also supports advanced resource handling such as cache busting and version‑agnostic URLs for WebJars. Adding webjars-locator-core enables version‑agnostic URLs, e.g., /webjars/jquery/jquery.min.js becomes /webjars/jquery/3.6.0/jquery.min.js .
Cache busting configuration
<code>spring:
web:
resources:
chain:
strategy:
content:
enabled: true
paths: "/**"</code>Static resource handling internals
Spring MVC creates a ResourceHandlerRegistry bean in WebMvcConfigurationSupport . The registry registers ResourceHandlerRegistration objects, which are turned into ResourceHttpRequestHandler instances and mapped via SimpleUrlHandlerMapping . The HttpRequestHandlerAdapter adapts these handlers for the MVC dispatch chain.
Key source code snippets illustrate the creation of the registry, conversion to handlers, and registration of the HandlerMapping .
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.