Spring Boot: v3.1.4

Hi all, I have a spring boot based mvc web application running. When i try to access a demo.css file http://localhost:8080/css/demo.css, I got the result in "No mapping for GET /css/demo.css". My project structure for static resource is resources/static/css/demo.css

To resolve the "No Mapping for GET" error, i add the below configuration, then error resolved.

My question: It is very common to have static files (such as css or js) to be accessed. Somemore, the css file (in this example) is placed under the default folder resources/static. Given the capabilities of spring boot, why do we still need to write so many codes just to allow the static files to be accessible. Can spring boot be enhanced to autoconfigure the below, so that developers do not have to spend effort to write the below configuration.

@Configuration
public class DemoSecurityConfig  extends WebMvcConfigurationSupport {


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

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

Comment From: wilkinsona

Spring Boot's auto-configuration will already serve such static resources by default. I suspect you have disabled this auto-configuration by using @EnableWebMvc or sub-classing WebMvcConfigurationSupport. If that's not the case, please provide a complete yet minimal sample that reproduces the problem and we can re-open the issue to investigate.

Comment From: bclozel

This is supported by default. Maybe your application overrides Spring Boot's opinion because it adds an "@EnableWebMvc" annotation somewhere or you are declaring your own "WebMvcConfigurationSupport" configuration class?

See https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.spring-mvc.auto-configuration and https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.spring-mvc.static-content

If it's not the case please provide a sample application that demonstrates the issue.

Comment From: hannah23280

Hi,

You are right. I have another configuration class that extends WebMvcConfigurationSupport , never realize this will cause Spring boot to back off the autoconfiguration of static resources.

Thanks! Stuck for many days cos of this issue.