Spring Boot - Override index page from webjar

对着背影说爱祢 提交于 2019-12-12 10:11:59

问题


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.



来源:https://stackoverflow.com/questions/27836584/spring-boot-override-index-page-from-webjar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!