正在将 WAR 文件部署到 Jetty:服务器为 运行,但应用程序未启动

Deploying WAR file to Jetty: server is running, but application didn't start

我得到 Spring 引导 2.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/

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

假设您有一个名为 MyRestApplication 的应用程序 class,您需要具备以下条件。

@SpringBootApplication
public class MyRestApplication {

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

假设你有 Jetty 作为依赖项以及 spring-boot-starter-web 你可以 运行 这个 class。

您还应该使用 spring 引导插件来创建 war 或 jar,然后您就可以启动它了。

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