It is already answered here .
It's not Spring Boot mapping to index.html it's the servlet engine (it's a welcome page). There's only one welcome page (per the spec), and directory browsing is not a feature of the containers.
You can manually add a view controller mapping to make this work:
@Configuration
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/emp").setViewName("redirect:/emp/"); //delete these two lines if looping with directory as below
registry.addViewController("/emp/").setViewName("forward:/emp/index.html"); //delete these two lines if looping with directory as below
String[] directories = listDirectories("/");
for (String subDir : directories){
registry.addViewController(subDir).setViewName("redirect:/" + subDir + "/");
registry.addViewController(subDir).setViewName("forward:/" + subDir + "/index.html");
}
super.addViewControllers(registry);
}
}
/**
* Resources mapping
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/assets/css/"); //my css are in src/resources/assets/css that's why given like this.
registry.addResourceHandler("/emp/**").addResourceLocations("classpath:/emp/");
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/assets/images/");
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/assets/js/");
}
/***
This should list subdirectories under the root directory you provide.
//Not tested
***/
private String[] listDirectories(String root){
File file = new File(root);
String[] directories = file.list(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
});
}
The first mapping causes Spring MVC to send a redirect to the client if /emp (without trailing slash) gets requested. This is necessary if you have relative links in /emp/index.html. The second mapping forwards any request to /emp/ internally (without sending a redirect to the client) to the index.html in the emp subdirectory.
And the error message there is no default error page you are getting because, you are trying to access localhost:8080/emp/ which is not yet configured. So spring-boot will check for any error page configured or not. As you haven't configured the error page too you are getting the above mentioned error.