Mapping rules of static resources in SpringBoot

To be familiar with and master the development, you must understand the principle of automatic configuration learned before!

For example, what does SpringBoot configure for us? Can we change it? What configurations can we modify? Can we expand?

Automatically configure components into containers: * * * Autoconfiguration

Automatic configuration class, encapsulating the contents of the configuration file: * * * Properties

Static resource mapping rules

In spring boot, the web configuration of spring MVC is in the configuration class webmvcoautoconfiguration;

We can see that there are many configuration methods in the internal class webmvccautoconfigurationadapter;

There is one method: addResourceHandlers add resource handlers

 public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) { 
               // The default configuration is spring. Web. Resources. Add mappings = true
               // Whether to enable the default resource processing. The default value is true
               //false means that default resource processing is disabled 
                logger.debug("Default resource handling disabled");
            } else {
                // webjars configuration
                this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
                // Static resource allocation
                this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
                    registration.addResourceLocations(this.resourceProperties.getStaticLocations());
                    if (this.servletContext != null) {
                        ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
                        registration.addResourceLocations(new Resource[]{resource});
                    }

                });
            }
        }

What is webjars?

The essence of Webjars is to introduce our static resources in the form of jar package. We used to import a static resource file directly.

Webjars is required to use SpringBoot. We can search:

Website: https://www.webjars.org

To use jQuery, we just need to introduce the pom dependency of the corresponding version of jQuery!

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

Access: as long as it is a static resource, SpringBoot will go to the corresponding path to find the resource. We visit here: http://localhost:8998/webjars/jquery/3.6.0/jquery.js

That is, when you use the path of webjars to access the file, SpringBoot will go to the path of class file / META-INF/resources/webjars / to find the file.

The second static resource mapping rule

How can we import our own static resources in our project? Let's look at the next line of code;

We go to the WebMvcProperties class of the mvcProperties variable to find the return value of the getStaticPathPattern () method,

 this.staticPathPattern = "/**";

 public String getStaticPathPattern() {
        return this.staticPathPattern;
    }

We go to staticPathPattern and find the second mapping rule: / * *. When accessing any Resource of the current project, it will find the Resource class. We can click to see the analysis:

Let's go to the Resources class of the resourceProperties variable (which is the internal class of WebProperties) to find the return value of the getStaticLocations () method

  private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

  this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
  
 public String[] getStaticLocations() {
            return this.staticLocations;
        }

Resource can set parameters related to our static resources; This points to the folder where it will look for resources, that is, the contents of the above array.

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"

We can create a new folder under the root directory of resources to store our static files;

For example, we visit http://localhost:8080/1.js , he will go to these folders to find the corresponding static resource files;

Custom static resource path

We can also specify which folders we need to put static resource files through the configuration file, and configure them in application.properties;

spring.web.resources.static-locations=classpath:/koma/,classpath:/ouring/


After configuring here, the default value of staticLocations will be changed, and the original four default paths will be changed to the paths we configure now.

public void setStaticLocations(String[] staticLocations) {
            this.staticLocations = this.appendSlashIfNecessary(staticLocations);
            this.customized = true;
        }

Reference catalogue

Mad God
https://www.cnblogs.com/hellokuangshen/p/12509918.html
Station B crazy God
https://www.bilibili.com/video/BV1PE411i7CV?p=4

The official SpringBoot document describes and uses the spring.web.resources.add-mappings configuration properties
https://docs.spring.io/spring-boot/docs/2.5.4/reference/htmlsingle/#application-properties.web.spring.web.resources.add-mappings

Tags: Spring Spring Boot

Posted on Sun, 19 Sep 2021 03:23:52 -0400 by wezalmighty