首页 > 解决方案 > Vaadin 10/11 和嵌入式 Jetty

问题描述

我在 Vaadin 8.5.1 上开发了中型应用程序。Jetty 嵌入式 9.4.8 用作 Vaadin servlet 的 Servlet 容器。在 Java 代码中,我初始化 Jetty 实例,创建 Vaadin servlet 并将其附加到 Jetty。在 Maven 中,我使用“vaadin-maven-plugin”,它可以帮助我对文件夹进行正确设置,包装也是“jar”。Spring(不是 Spring Boot)用于应用程序配置目的和 IoC。

现在我想将项目迁移到 Vaadin 10/11。我尝试了所有生成输出 JAR 的 Vaadin Starter Packs。但不明白如何修改这些包以删除 Spring Boot 并获得一个嵌入 Jetty 的简单 Maven 项目。

已在 Vaadin 论坛中提出问题: Vaadin 10 + Jetty Embedded

标签: javaspringmavenembedded-jettyvaadin10

解决方案


您必须按如下方式配置 Jetty 服务器:

public class Application {

    public static void main(String... args) throws Exception {
        new Application().run(8080, "/");
    }

    public void run(int port, String contextPath) throws Exception {
        URL webRootLocation = this.getClass().getResource("/META-INF/resources/");
        URI webRootUri = webRootLocation.toURI();

        WebAppContext context = new WebAppContext();
        context.setBaseResource(Resource.newResource(webRootUri));
        context.setContextPath(contextPath);
        context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*");
        context.setConfigurationDiscovered(true);
        context.setConfigurations(new Configuration[]{
                new AnnotationConfiguration(),
                new WebInfConfiguration(),
                new WebXmlConfiguration(),
                new MetaInfConfiguration(),
                new FragmentConfiguration(),
                new EnvConfiguration(),
                new PlusConfiguration(),
                new JettyWebXmlConfiguration()
        });
        context.getServletContext().setExtendedListenerTypes(true);
        context.addEventListener(new ServletContextListeners());

        Server server = new Server(port);
        server.setHandler(context);

        server.start();
        server.join();
    }

}

此外,maven-shade-plugin如果要将工件打包为 uber-jar,则需要使用 。

您可以在https://github.com/alejandro-du/embedded-jetty-demo找到 Vaadin 12+ 的示例


推荐阅读