2
votes

Cracking head over this. The documentation says:

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext.

Minimal example:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableWebMvc
public class Main {
    public static void main(final String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

And create one directory /src/main/resources/public placing static resources there. When I run the application I only get 404. When I remove @EnableWebMvc resources are served as expected.

Attempted (and failed) remedies

/**
 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
 */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/public/**").addResourceLocations("/public/");
}

In the application.properties added:

spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

The question

So my question: What do I need to configure to serve static resource when I want to use the @EnableWebMvc annotation?

2
Seems like @EnableWebMvc doesn't work well with Spring Boot. See: techblog.bozho.net/spring-boot-enablewebmvc-common-use-casesstwissel

2 Answers

1
votes

You should configure a ViewResolver something like below along with your ResourceHandlers. Check this

@Bean
public InternalResourceViewResolver defaultViewResolver() {
   return new InternalResourceViewResolver();
}
0
votes

In the documentation you mentionned, it says :

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

You should try to use @EnableWebMvc with your configuration instead of your Spring boot application. There's an example of this in this documentation.

Enabling the MVC Java Config or the MVC XML Namespace

To enable MVC Java config add the annotation @EnableWebMvc to one of your @Configuration classes:

@Configuration
@EnableWebMvc
public class WebConfig {
}

Also in these examples :

I hope this will help you.