Spring Boot WAR包无法在Tomcat部署,API访问404求助
Spring Boot WAR包部署Tomcat后404问题排查
已将Spring Boot应用打包为WAR包部署到Tomcat,WAR包在Tomcat中可见,但所有API接口访问均返回404错误,无法正常运行。相关配置如下:
build.gradle配置
plugins { id 'java' id 'war' id 'org.springframework.boot' version '3.2.5' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.application.chatsystem' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' } tasks.named('test') { useJUnitPlatform() } war { archivesBaseName = 'socialmedia' } task build(overwrite: true) { dependsOn = ['clean','test','war'] test.mustRunAfter clean war.mustRunAfter test }
Application类代码
package com.application.chatsystem.socialmedia; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Controller类代码
package com.application.chatsystem.socialmedia.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class StatusController { @GetMapping("/rest/status") public ResponseEntity<String> getStatus() { return ResponseEntity.ok("Working"); } }
Tomcat server.xml配置
<Server port="-1" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener"/> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/> <Service name="Catalina"> <Connector port="8080" maxHttpHeaderSize="8192" connectionTimeout="20000" maxThreads="2000" enableLookups="false" URIEncoding="utf-8" /> <Engine name="Catalina" defaultHost="localhost"> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="/" docBase="ROOT" reloadable="true"/> </Host> </Engine> </Service> </Server>
问题排查与解决方案
1. 修正接口访问路径
你的WAR包名为socialmedia.war,Tomcat默认会将其部署到/socialmedia上下文路径下。当前访问地址缺少该路径,导致404:
- 正确访问地址:
http://localhost:8080/socialmedia/rest/status
若需将应用部署在根路径/,可二选一:
- 将WAR包重命名为
ROOT.war,替换Tomcat默认的ROOT应用 - 修改
server.xml的Context配置:<Context path="/" docBase="socialmedia" reloadable="true"/>
2. 检查Tomcat版本兼容性
Spring Boot 3.2.5基于Jakarta EE 9+,要求Tomcat版本为10.1.x及以上。若使用Tomcat 9或更低版本,会因API不兼容导致应用无法初始化,出现404。
3. 验证WAR包完整性
- 解压WAR包,确认
WEB-INF/classes下存在编译后的Application.class和StatusController.class - 确认
WEB-INF/lib-provided目录包含spring-boot-starter-tomcat依赖(该依赖已设为providedRuntime,Tomcat会提供自身容器实现)
4. 查看Tomcat启动日志
检查Tomcatlogs目录下的catalina.out或localhost.log,排查是否存在应用启动异常(如类加载失败、依赖缺失等),这些异常会导致Spring容器无法正常初始化,进而引发404。
5. 确认SpringBootServletInitializer配置
你的Application类已正确继承SpringBootServletInitializer并实现configure方法,此配置符合WAR部署要求,无需修改。
内容的提问来源于stack exchange,提问作者anuja tatpuje
相关产品推荐
相关产品推荐

