相关文章推荐
安静的手链  ·  C++ ...·  8 月前    · 
闯红灯的松球  ·  Android TextView ...·  1 年前    · 
安静的西瓜  ·  php curl ...·  1 年前    · 
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");
                Note that, in most regular apps, one would not use file:... but classpath:/here/some/path/. This is because all resources are zipped in a .jar (or .war) and files are not on the filesystem in the deployed environment, and the file:... reference won't work anymore, unless specific circumstances.
– Adrien
                Jan 5, 2021 at 9:46
                If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.
– DerM
                Jul 17, 2018 at 13:33
        

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.