Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 919 Bytes

静态资源映射.md

File metadata and controls

26 lines (21 loc) · 919 Bytes

SpringBoot映射本地静态资源

逻辑

  1. 实现org.springframework.web.servlet.config.annotation.WebMvcConfigurer接口
  2. 重写void addResourceHandlers(ResourceHandlerRegistry registry)方法
  3. 添加资源映射

代码

public class DeployServerApplication implements WebMvcConfigurer {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // 配置静态资源映射
    registry.addResourceHandler("/**/*.html", "/**/*.css", "/**/*.js")
        // 资源字符串支持标准的java url格式,分别为 classpath: 或 file:
        .addResourceLocations("classpath:/static/");
    registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
  }
}