Spring 引导 - 从 webjar 覆盖索引页
Spring Boot - Override index page from webjar
在我的项目中,我使用了 swagger-ui 库,它在 class 路径的根目录中有 index.html 文件。以这种方式,当我像 /
一样点击 root url 时,这个 index.html
成为我的应用程序的起始页。
但我想使用我的 Boot 项目 resources/templates
文件夹中的自定义 Groovy 模板 index.tpl
。当我执行这种方法时,应用程序仍然显示来自 Swagger-UI JAR 文件的 index.html
。
如何用项目中的自定义页面覆盖 jar 中的索引页?
UPD:下面的方法对我不起作用。它 returns 404 错误。然后我添加 @EnableWebMvc 注释,现在 Spring 找不到我的 Groovy 模板。我在 class 路径中为 Groovy 模板提供了所有必要的依赖项,并且它们在属性文件中已打开。似乎 Spring 根本无法解析 Groovy 模板。
Spring Boot 的 WebMvcAutoConfigurationAdapter 默认注册从“/”到“/index.html”的转发(在方法 addStaticIndexHtmlViewControllers 中)。因此,您必须在路径“/index.html”下注册视图。
这可以通过控制器上的 @RequestMapping("/index.html")
或以下方式完成:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter
{
@Override
public void addViewControllers(ViewControllerRegistry registry)
{
registry.addViewController("/index.html").setViewName("index");
}
}
另一种选择是覆盖 WebMvcAutoConfigurationAdapter
并禁用 WebMvcAutoConfiguration
。
在我的项目中,我使用了 swagger-ui 库,它在 class 路径的根目录中有 index.html 文件。以这种方式,当我像 /
一样点击 root url 时,这个 index.html
成为我的应用程序的起始页。
但我想使用我的 Boot 项目 resources/templates
文件夹中的自定义 Groovy 模板 index.tpl
。当我执行这种方法时,应用程序仍然显示来自 Swagger-UI JAR 文件的 index.html
。
如何用项目中的自定义页面覆盖 jar 中的索引页?
UPD:下面的方法对我不起作用。它 returns 404 错误。然后我添加 @EnableWebMvc 注释,现在 Spring 找不到我的 Groovy 模板。我在 class 路径中为 Groovy 模板提供了所有必要的依赖项,并且它们在属性文件中已打开。似乎 Spring 根本无法解析 Groovy 模板。
Spring Boot 的 WebMvcAutoConfigurationAdapter 默认注册从“/”到“/index.html”的转发(在方法 addStaticIndexHtmlViewControllers 中)。因此,您必须在路径“/index.html”下注册视图。
这可以通过控制器上的 @RequestMapping("/index.html")
或以下方式完成:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter
{
@Override
public void addViewControllers(ViewControllerRegistry registry)
{
registry.addViewController("/index.html").setViewName("index");
}
}
另一种选择是覆盖 WebMvcAutoConfigurationAdapter
并禁用 WebMvcAutoConfiguration
。