Spring boot changing static path pattern not serving index.html automatically

Viewed 427

I have a spring boot project where i have included front end code as well in resources/static/ folder. I have set the context path as /ibo-modules and port as 9080. Now when i try to hit http://localhost:9080/ibo-modules/, spring boot will automatically load the index.html file from that. Now i want to change the url pattern for loading static files. For that, i tried two methods:

  1. Setting spring.mvc.static-path-pattern=/ui/**
  2. Created custom webMvcConfigurer as below
public class CustomWebMvcConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/ui/**").addResourceLocations("classpath:/static/");
    }
}

In both cases, whenever i hit http://localhost:9080/ibo-modules/ui/ it is not loading. I am getting 404 error. I had to explicitly enter http://localhost:9080/ibo-modules/ui/index.html to load the static page. But in my project, this is not intended. Can anyone help me resolve this issue?

1 Answers

As you did with the WebMvcConfigurer.addResourceHandlers method you can equally override the WebMvcConfigurer.addViewControllers method with the special forward: prefix like below:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/ui/") //<--Configure automated controller for /ui/
            .setViewName("forward:/ui/index.html"); //<-- forward to index.html
}
Related