How to change the way Spring Boot serves static files?

后端 未结 2 1394
萌比男神i
萌比男神i 2021-01-14 06:50

After using JHipster on a couple of new projects recently (Highly recommended ! Amazing work !), I am trying to back-port some of the concepts into an older webapp, essentia

相关标签:
2条回答
  • 2021-01-14 07:03

    you can add following code in application.propertise:

    spring.resources.static-locations=classpath:/webapp/
    

    and following code in application.yaml:

    resources:
        static-locations: classpath:/webapp/
    
    0 讨论(0)
  • 2021-01-14 07:07

    You can try following configuration. The idea should be pretty straight forward. It does register artifacts in assets directory.

    public class AppMvcConfiguration extends WebMvcConfigurerAdapter {
    
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          // Including all static resources.
    
          registry.addResourceHandler("/assets/**", 
                      "/css/**", 
                      "/img/**",
                      "/js/**"
                 ).addResourceLocations("/assets/",
                      "/css/", 
                      "/img/",
                      "/js/"
          ).resourceChain(true)
           .addResolver(new PathResourceResolver());
    
           super.addResourceHandlers(registry);
      }
    }
    
    0 讨论(0)
提交回复
热议问题