2

在我的项目中,我使用 swagger-ui 库,该库在类路径的根目录中有 index.html 文件。这样index.html,当我点击 root url 时,这将成为我的应用程序的起始页面/
但我想使用我的 Boot 项目文件夹中的index.tpl自定义 Groovy 模板。resources/templates当我执行这种方法时,应用程序仍然index.html从 Swagger-UI JAR 文件中显示。


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

UPD:下面的方法对我不起作用。它返回 404 错误。然后我添加 @EnableWebMvc 注释,现在 Spring 找不到我的 Groovy 模板。我的类路径中有 Groovy 模板的所有必要依赖项,它们在属性文件中打开。似乎 Spring 根本无法解析 Groovy 模板。

4

1 回答 1

2

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.

于 2015-01-08T10:46:48.600 回答