Home  >  Article  >  Java  >  What are SpringBoot static resource mapping rules?

What are SpringBoot static resource mapping rules?

PHPz
PHPzforward
2023-05-13 16:28:061202browse

1. Static resource mapping rules

Double-click shift or ctrl N in the project to search for the WebMvcAutoConfiguration.class file. The addResourceHandlers method is as follows:

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
        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});
            }
        });
    }
}

Then enter the getStaticLocations() method and you can find the value of the variable staticLocations as follows:

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

That is, when the project is run, the above path will be reached Search for static resources, or you can customize the static resource path, which needs to be configured in application.properties:

spring.resources.static-locations=classpath:/folder1/,classpath:/folder2/

Note: Once the path to the static folder is customized, the default static resource path will become invalid.

2. Welcome page

The index.html file under the static resource path will be mapped by /** when accessing http://localhost:8080/ , will be mapped to index.html in the static resource folder by default.

Problems encountered

After creating the index.html file and running the project, a page error will occur when accessing http://localhost:8080/:

What are SpringBoot static resource mapping rules?

The console reports the following error:

What are SpringBoot static resource mapping rules?

The version of Spring Boot is 2.7.8 and the version of tomcat is 9.0.71. Spring Boot runs the project through embedded tomcat, but it needs to rely on the local java environment. My local java version is Java 1.8.0_261 (that is, java 8 version). Generally, java 8 and tomcat 8.x.x are used together. This may be Problems caused by version conflicts. Change the project's SDK to jbr-11 JetBrains Runtime version 11.0.10 to solve the problem:

What are SpringBoot static resource mapping rules?

JetBrains Runtime can be considered as the one that comes with IDEA java runtime environment.

The above is the detailed content of What are SpringBoot static resource mapping rules?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete