spring boot 静态资源的映射规则 (1) webjars 资源映射

我们可以在   WebMvcAutoConfiguration 这个类下查找一个方法    addResourceHandlers

  public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }

从这段代码我们可以看出 如果收到    /webjars/**    请求 , 我们会去    classpath:/META-INF/resources/webjars/  这里查找资源文件

比如 我们 可以 去  https://www.webjars.org/ 这个官网  引入jquery  

添加依赖到pom.xml 文件下

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.3.1</version>
</dependency>

 

 

访问 http://localhost:8080/webjars/jquery/3.3.1/jquery.js 

得到 

 

posted @ 2020-03-01 11:32  1点  阅读(256)  评论(0编辑  收藏  举报