Spring boot static files load 404

Reason: does not block static resources before version 1.5, 2.x + version interception static resources

Solution:


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class StaticSourceConfig implements WebMvcConfigurer {
    
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/"};


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        try {
            if (!registry.hasMappingForPattern("/webjars/**")) {
                registry.addResourceHandler("/webjars/**").addResourceLocations(
                        "classpath:/META-INF/resources/webjars/");
            }
            if (!registry.hasMappingForPattern("/**")) {
                registry.addResourceHandler("/**").addResourceLocations(
                        CLASSPATH_RESOURCE_LOCATIONS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

List:
Here Insert Picture Description
Here Insert Picture Description
loaded successfully. . .
Here Insert Picture Description

Published 22 original articles · won praise 9 · views 7657

Guess you like

Origin blog.csdn.net/qq_35719898/article/details/103832205