Issue
I'm new to Java and to Spring.
How can I map my app root http://localhost:8080/
to a static index.html
?
If I navigate to http://localhost:8080/index.html
its works fine.
My app structure is :
src="https://i.stack.imgur.com/bMEaw.png" alt="dirs">
My config\WebConfig.java
looks like this:
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}
I tried to add registry.addResourceHandler("/").addResourceLocations("/index.html");
but it fails.
Solution
It would have worked out of the box if you hadn't used @EnableWebMvc
annotation. When you do that you switch off all the things that Spring Boot does for you in WebMvcAutoConfiguration
. You could remove that annotation, or you could add back the view controller that you switched off:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
Answered By - Dave Syer
Answer Checked By - Timothy Miller (JavaFixing Admin)