【问题标题】:Spring Boot - Override index page from webjarSpring Boot - 从 webjar 覆盖索引页面
【发布时间】:2015-03-06 08:36:26
【问题描述】:

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


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

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

【问题讨论】:

    标签: groovy spring-boot swagger-ui webjars


    【解决方案1】:

    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

    【讨论】:

    • 谢谢@koe,但这种方法对我不起作用。它返回 404。然后我添加 @EnableWebMvc 注释,现在 Spring 找不到我的 Groovy 模板。
    • 我自己尝试了第一个选项,它奏效了。您是否尝试使用覆盖 WebMvcAutoConfigurationAdapter?
    • 感谢您的帮助!那是我的错。似乎 IntellijIdea 在识别 Groovy 模板方面存在问题,无法解析我的 Groovy 视图 :(
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2019-08-29
    • 2017-11-15
    • 2015-05-01
    • 2018-10-11
    • 2020-06-21
    • 2015-04-07
    • 2017-06-18
    相关资源
    最近更新 更多