SoFunction
Updated on 2025-04-08

The problem and solution is that springBoot static resources cannot be loaded and cannot be valid if configured.

Problem description, and history

When I was writing the project today, I imported a front-end project. When I loaded it, I found that it was 404. I guess I didn’t scan the static resource. I just wrote a configuration class like before:

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        ("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/templates/");
        (registry);
    }
}

Then it was found that it did not take effect.

I searched and found various configuration methods, inheriting this class or implementing that method. I searched a lot of them online, but they didn't work. Then I tried to use configuration file configuration, that is, yml. Because I used the thymeleaf engine template, I also configured some thymeleaf configurations. It can be better, at least I can access the html on the homepage, but the static resources still cannot be loaded.

There is also the filtered pom file, just delete that. I checked that problem, but I don’t have that filter.

Solve the problem

After an afternoon of troubleshooting and searching, I finally found a solution: The problem with the springBoot version I used: 2. There will be problems and the configuration needs to be introduced before it can take effect:

Springboot-2. The default path matching strategy used by spring mvc-5.

The Spring MVC handler maps the default policy for matching the request path has been derived fromAntPathMatcherChange toPathPatternParser

So we have to set it back for him

spring:
      mvc:
        pathmatch:
          matching-strategy: ant_path_matcher

Then use the previous methods and it will be OK, which one takes effect.

Or, directly reduce the boot version.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.