【问题标题】:configure jetty in spring boot在 Spring Boot 中配置码头
【发布时间】:2015-02-07 04:22:59
【问题描述】:

我正在开发一个多模块 Spring Boot 项目。我的项目结构看起来像

  • 我的项目(父)
  • 前端
    • src/main/资源
      • 前端
        • index.html
  • 休息
    • src/main/java
      • com.example
        • MyWebApp.java
      • com.example.config
        • WebAppConfig.java

我正在尝试通过将 JettyServerCustomizer 作为 bean 注入 WebAppConfig 来配置码头,如下所示

@Bean
public JettyServerCustomizer customizeJettyServer()
{
    return new JettyServerCustomizer()
    {

        @Override
        public void customize(final Server server)
        {
            ContextHandler frontEndContext = new ContextHandler();
            frontEndContext.setContextPath(""); //what should be here
            frontEndContext.setResourceBase("");//what should be here
            ResourceHandler frontEndResourceHandler = new ResourceHandler();
            frontEndResourceHandler.setWelcomeFiles(new String[] { "index.html" });
            frontEndContext.setHandler(frontEndResourceHandler);
            ContextHandlerCollection contexts = new ContextHandlerCollection();
            contexts.setHandlers(new Handler[] { frontEndContext});
            server.setHandler(contexts);
        }
    };
}

contextPathResourceBase 设置什么值,以便我可以运行前端模块中的index.html?网址会是什么样子?

谢谢你:)

【问题讨论】:

    标签: jetty spring-boot


    【解决方案1】:

    Spring Boot 可以serve static content 为您服务。与其尝试配置 Jetty,不如将您的静态内容放在 src/main/resources/static 下,它们将直接从类路径加载。

    【讨论】:

    • 当您使用./gradlew bootRun 运行应用程序时确实如此,但是当您构建独立 jar 时,如何指定静态内容的位置,因为在 JAR 部署中,src/main/resources/static 文件夹是你的类路径中没有吗?
    • 该特定文件夹不在类路径中,但 static 文件夹及其内容与构建系统包含在 jar 中一样。
    猜你喜欢
    • 2017-05-04
    • 2021-06-14
    • 2015-07-27
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2018-06-12
    • 2016-08-22
    • 2019-08-26
    相关资源
    最近更新 更多