1

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 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.