macOS(M1)下IntelliJ IDEA UE配置Tomcat9遇HTTP 404问题求助
问题描述
我fork了一个Java项目,按常规步骤配置Tomcat 9后,启动服务器出现HTTP Status 404错误。环境:macOS M1(2020款MacBook Air),安装了Tomcat 9和10,当前使用Tomcat 9搭配IntelliJ IDEA旗舰版。
项目相关截图
- Tomcat Server设置

- Tomcat部署配置

- 浏览器404错误页面

- Artifact配置

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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>filter</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>5.8.2</junit.version> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.12.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.12.4</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <configuration> <includes> <include>**/*Test.java</include> </includes> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </build> </project>
解决步骤
1. 修正访问URL与上下文路径匹配
从部署截图看,项目的上下文路径是/filter-1.0-SNAPSHOT,但浏览器直接访问了http://localhost:8080/,缺少项目路径。两种解决方式:
- 直接访问完整路径:
http://localhost:8080/filter-1.0-SNAPSHOT/ - 修改Tomcat部署配置:在Application context栏输入
/,将项目设为根路径,之后直接访问http://localhost:8080/即可。
2. 重新构建并验证Artifact
执行mvn clean package命令清理并重新构建项目,确保编译后的文件正确生成在target/filter-1.0-SNAPSHOT目录下。同时确认IDEA中部署的Artifact是filter:war exploded(已展开的war包),避免打包异常导致的文件缺失。
3. 检查web.xml欢迎页配置
确认项目WEB-INF/web.xml中配置了默认欢迎页,例如:
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list>
如果没有配置默认页,访问根路径会返回404。
4. 确保Tomcat与依赖版本兼容
pom.xml中使用的javax.servlet-api:4.0.1与Tomcat 9完全兼容(Tomcat 9对应Servlet 4.0规范),注意不要混用Tomcat 10的依赖(Tomcat 10使用jakarta.servlet命名空间,与javax.servlet不兼容),IDEA中务必选择Tomcat 9作为服务器SDK。
5. 清理缓存并重启
- 清理IDEA缓存:
File -> Invalidate Caches...,选择Invalidate and Restart - 清理Tomcat工作目录:删除Tomcat安装目录下
work文件夹内的所有内容,再重启Tomcat服务器
6. 检查端口占用情况
在终端执行lsof -i :8080查看8080端口是否被其他程序占用,若被占用,修改Tomcat配置文件conf/server.xml中的端口号(例如改为8081)。
内容的提问来源于stack exchange,提问作者Sarvar Khalimov

