Spring Boot - Override index page from webjar

2019-05-23 08:08发布

问题:

In my project I use swagger-ui library which have index.html file in the root of class path. In such way this index.html becomes the start page of my app when I hit root url like /.
But I want to use my custom Groovy template index.tpl from resources/templates folder of my Boot project. When I perform such approach application still displays index.html from Swagger-UI JAR file.


How to override index page from jar with custom one from project?

UPD: Approach below doesn't work for me. It returns 404 error. Then I add @EnableWebMvc annotation and now Spring can't find my Groovy Template. I have all necessary dependencies in my classpath for Groovy Template and they are turned on in the properties file. Seems like Spring can't resolve Groovy Template at all.

回答1:

Spring Boot's WebMvcAutoConfigurationAdapter registers the forward from "/" to "/index.html" by default (in method addStaticIndexHtmlViewControllers). Therefore you have to register the view under the path "/index.html".

This can be done with @RequestMapping("/index.html") on the controller or with:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter
{
    @Override
    public void addViewControllers(ViewControllerRegistry registry)
    {
        registry.addViewController("/index.html").setViewName("index");
    }
}

Another option would be to override WebMvcAutoConfigurationAdapter and disable WebMvcAutoConfiguration.