The spring.resources.static-locations property told Spring Boot which folders to serve static files from, replacing the default list of classpath folders. Since Spring Boot 2.4 it has been renamed to spring.web.resources.static-locations, and the old name no longer works on current Spring Boot versions, so use the new key.
Spring Boot serves static content such as HTML, CSS, JavaScript and images straight from the classpath without any controller code. This property controls where it looks.
By default Spring Boot serves static resources from these classpath locations, in order:
classpath:/META-INF/resources/classpath:/resources/classpath:/static/classpath:/public/A file at src/main/resources/static/css/site.css is therefore available at /css/site.css. The static-locations property replaces this list. Anything you set must be a resource location, usually with a classpath: or file: prefix and a trailing slash for directories.
Spring Boot 2.4 moved the resource settings from spring.resources.* into the spring.web.resources.* namespace, because they are not specific to Spring MVC and also apply to WebFlux. The old names were deprecated and have since been dropped.
| Old name | Current name |
|---|---|
spring.resources.static-locations | spring.web.resources.static-locations |
spring.resources.add-mappings | spring.web.resources.add-mappings |
spring.resources.cache.period | spring.web.resources.cache.period |
spring.resources.chain.* | spring.web.resources.chain.* |
If you upgrade an old application and your static files suddenly return 404, check whether the old property is still in application.properties. The spring-boot-properties-migrator module reports renamed keys at startup and is useful during an upgrade.
Remember that this value replaces the defaults. If you set only classpath:/assets/, files in static/ stop being served. Include the defaults you still need, as shown above.
The same setting in YAML:
A file: location lets you serve files from outside the jar, for example user uploads. Make sure the application has read permission, never point it at a directory with secrets, and put it behind authentication where needed. For frequently changing uploads, a controller or a CDN or object store is often a better fit.
The URL pattern defaults to /** through spring.mvc.static-path-pattern. Setting a prefix keeps static files separate from API routes.
With this, static/app.js is served at /assets/app.js with a cache lifetime of seven days. The content strategy adds a hash to the file name in resolved URLs so browsers can cache aggressively and still pick up changes. Use the ResourceUrlProvider-aware helpers of your template engine to generate those URLs.
To turn static resource mapping off completely, for example in a pure API application:
When mappings are disabled, requests for unmatched paths reach your exception handling instead of the resource handler. Webjars under /webjars/** are also governed by this setting.
When properties are not enough, register your own handler with WebMvcConfigurer. Note that this adds to Boot's behaviour for the paths you map.
Do not annotate the class with @EnableWebMvc, because that switches off Spring Boot's MVC auto-configuration, including the static resource defaults.
The most common reason is that the old spring.resources.static-locations key is no longer recognised. Rename it to spring.web.resources.static-locations.
It replaces them. List every location you still want, including classpath:/static/ if you use it.
Yes, use a file: URL such as file:/var/www/uploads/ with a trailing slash. Check permissions and security before exposing it.
Use src/main/resources/static. It is the conventional location, works without configuration and is documented by Spring Boot.
spring.web.resources.static-locations (formerly spring.resources.static-locations) defines the folders Spring Boot serves static files from and replaces the default META-INF/resources, resources, static and public list. Use the new property name on current versions, keep the defaults you need, add a path prefix and caching with the related settings, and use WebMvcConfigurer for anything more advanced.