Spring Boot-从Webjar覆盖索引页面 [英] Spring Boot - Override index page from webjar

查看:150
本文介绍了Spring Boot-从Webjar覆盖索引页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用了swagger-ui库,该库在类路径的根目录中具有index.html文件.这样,当我按下/之类的根URL时,此index.html便成为我应用程序的起始页.
但是我想使用Boot项目的resources/templates文件夹中的自定义Groovy模板index.tpl.当我执行这种方法时,应用程序仍显示Swagger-UI JAR文件中的index.html.

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.


如何用项目中的自定义项覆盖jar中的索引页?


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

UPD:以下方法对我不起作用.它返回404错误.然后添加@EnableWebMvc批注,现在Spring找不到我的Groovy模板.我的Groovy模板的类路径中具有所有必需的依赖关系,并且已在属性文件中将其打开.似乎Spring根本无法解析Groovy模板.

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.

推荐答案

默认情况下,Spring Boot的WebMvcAutoConfigurationAdapter在"addStaticIndexHtmlViewControllers"方法中将转发从"/"注册到"/index.html".因此,您必须在路径"/index.html"下注册视图.

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".

这可以通过控制器上的@RequestMapping("/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");
    }
}

另一种选择是覆盖WebMvcAutoConfigurationAdapter并禁用WebMvcAutoConfiguration.

Another option would be to override WebMvcAutoConfigurationAdapter and disable WebMvcAutoConfiguration.

这篇关于Spring Boot-从Webjar覆盖索引页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆