我有可运行的 springboot(2.4) Java(8) 项目,它与带有 .jar 扩展名的嵌入式 tomcat(10) 一起工作得很好。我需要在 tomcat 应用程序服务器上运行相同的项目,但在我将其更改为 war 并部署之后进入 tomcat 应用程序服务器,它给出 404 错误消息。我是否需要做一些额外的工作才能部署在 tomcat 应用程序服务器上。
我的主要课程如下,
@ComponentScan("com.example.testproject")
@SpringBootApplication
@EnableSwagger2
@Slf4j
@EnableAutoConfiguration
public class ApplicationStarter extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return configureApplication(builder);
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ApplicationStarter.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
return builder.sources(ApplicationStarter.class);
}
}
我还添加了以下依赖项,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<groupId>com.example</groupId>
<artifactId>testproject</artifactId>
<version>0.0.1-testproject</version>
<packaging>war</packaging> // earlier it was jar and with embedded tomcat its working
除了您已经执行的步骤之外,您还应该将其添加到
pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
通过定义
scope = provided
,您可以防止 Spring Boot 启动嵌入式 Tomcat。重新打包项目后,您可以将 WAR 文件复制到外部 Tomcat 并重新启动它。