Spring Boot项目Jar转War部署Tomcat8.5.9出现404错误求助
Spring Boot转War包部署Tomcat 8.5.9出现404问题排查及解决
问题描述
将Spring Boot应用从Jar包转换为War包后,部署到Tomcat 8.5.9服务器时出现404错误。
相关代码配置
启动类代码
@SpringBootApplication public class SpringApiApplication extends SpringBootServletInitializer implements WebApplicationInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringApiApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringApiApplication.class, args); } }
控制器代码
// 控制器代码 package com.javaproject.springapi.controller; import java.util.List; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.javaproject.springapi.model.Student; import com.javaproject.springapi.service.StudentService; @RestController @RequestMapping("/api") public class StudentController { private StudentService studentService; public StudentController(StudentService studentService) { super(); this.studentService = studentService; } @PostMapping() public ResponseEntity<Student> addStudent(@RequestBody Student student){ return new ResponseEntity<>(studentService.addStudent(student), HttpStatus.CREATED); } @GetMapping public List<Student> getStudents(){ return studentService.getStudents(); } // 根据ID查询学生 @GetMapping("{id}") public ResponseEntity<Student> getStudentById(@PathVariable("id") long stuId){ return new ResponseEntity<>(studentService.getStudentById(stuId), HttpStatus.OK); } // 更新学生信息 @PutMapping("{id}") public ResponseEntity<Student> updateStudent(@PathVariable("id") long id, @RequestBody Student student){ return new ResponseEntity<>(studentService.updateStudent(student, id), HttpStatus.OK); } // 删除学生 @DeleteMapping("{id}") public ResponseEntity<String> deleteStudent(@PathVariable("id") long id){ studentService.deleteStudent(id); return new ResponseEntity<>("学生删除成功!", HttpStatus.OK); } }
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> <relativePath/> </parent> <groupId>com.springboot.app</groupId> <artifactId>springboot-war-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot-war-demo</name> <description>Spring Boot WAR Demo</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
问题排查与解决
1. 版本兼容性问题(核心原因)
- Spring Boot 2.7.3依赖Servlet API 4.0,而Tomcat 8.5.9仅支持Servlet API 3.1,版本不匹配导致应用无法正常初始化。
- Java 17与Tomcat 8.5.9不兼容,Tomcat 8.5最高仅支持Java 11。
解决方法二选一:
- 升级Tomcat到9.0.x版本(推荐9.0.60+,支持Java 8-17,与Spring Boot 2.7.3兼容)。
- 降级Spring Boot到2.3.x及以下版本(支持Tomcat 8.5),同时将Java版本降至11或更低。
2. 访问路径错误
War包部署到Tomcat后,访问路径需包含War包名称,例如:
- 若War包名为
springboot-war-demo.war,正确访问地址应为http://localhost:8080/springboot-war-demo/api,而非直接访问http://localhost:8080/api。
3. 启动类优化
启动类无需额外实现WebApplicationInitializer,因为SpringBootServletInitializer已实现该接口,修改后代码如下:
@SpringBootApplication public class SpringApiApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringApiApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(SpringApiApplication.class, args); } }
4. 验证应用启动状态
查看Tomcat的logs/catalina.out日志,确认应用是否成功启动,若存在类加载异常、依赖缺失等错误,需针对性修复。
内容的提问来源于stack exchange,提问作者Shalini
相关产品推荐
相关产品推荐

