Reputation: 20384
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.
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.
/**
* @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/
So my question: What do I need to configure to serve static resource when I want to use the @EnableWebMvc
annotation?
Upvotes: 2
Views: 4028
Reputation: 4558
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.
Upvotes: 1