首页 > 解决方案 > 将 WAR 文件部署到 Jetty:服务器正在运行,但应用程序未启动

问题描述

我得到了 Spring Boot2.2.7.RELEASE应用程序。将其打包为war文件并尝试使用 Jetty 在本地部署它:

public static void startJetty() throws Exception {
    Server server = new Server(8080);
    WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    File warFile = new File("target/rest-service/my-rest-service.war");
    context.setWar(warFile.getAbsolutePath());
    server.setHandler(context);
    server.start();
    server.dumpStdErr();
    server.join();
}

在此 Jetty 启动并运行后localhost:8080,日志中没有错误,但似乎应用程序没有启动。当我尝试到达它时,响应是 404 NOT FOUND。虽然我确实看到了我的应用程序目录localhost:8080/swagger-ui/

标签: javaspring-bootjetty

解决方案


删除该代码并让 Spring Boot 处理它。

假设您有一个名为的应用程序类MyRestApplication,您需要具有以下内容。

@SpringBootApplication
public class MyRestApplication {

  public static void main(String[] args) {
    SpringApplication.run(MyRestApplication.class, args);
  }
}

假设您将 Jetty 作为依赖项,并且spring-boot-starter-web您可以运行此类。

您还应该使用 spring Boot 插件来创建战争或 jar,然后您可以启动它。

java -jar my-rest-service.war它会启动你需要的一切。


推荐阅读