Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a standard Spring Boot app and try to serve some static content. I want to change the location to a specific folder on the filesystem. These seem to be the most common approaches:
Set the path in application.yaml
spring:
resources:
static-locations: "file:/here/some/path"
Use WebMvcConfigurer
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("file:/here/some/path");
Here is my problem: the first approach works, the second does not and I can not figure out why. Any hints?
Is there a difference between setting "static-locations" and "addResourceLocations"? What would be a starting point to debug?
I would like to use the second one because I want to set the path depending on a specific condition.
Thanks!
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("file:/here/some/path");
BUT for some reason redirecting to index.html got disabled (which is quite some unexpected behaviour). So I needed to add it manually.
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("file:/here/some/path");
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.