Spring基于注解的Java配置无效,请求404问题求助
问题现象
学习Spring基于注解的Java配置时,配置始终不生效。提交表单后请求/SpringTesting/add返回404错误,无明确报错信息,仅收到Tomcat状态报告:
HTTP Status 404 – Not Found
Type Status ReportMessage The requested resource [/SpringTesting/add] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.95
使用Apache Tomcat 9.0.95作为服务器,相关代码与项目结构如下:
相关代码
AppConfig.java
package com.test.Configuration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration @ComponentScan("com.test") @EnableWebMvc public class AppConfig extends WebMvcConfigurationSupport { public AppConfig() { System.out.println("AppConfig has been initialized!"); } }
AppInitializer.java
package com.test.Configuration; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { AppConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
FirstController.java
package com.test.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class FirstController { @RequestMapping("/add") public void addNumbers(@RequestParam("val1") int val1, @RequestParam("val2") int val2) { System.out.println("Reached");//Debug Breakpoint here } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Web MVC Testing</title> </head> <body> <form action="add"> <input type="number" name="val1"><br> <input type="number" name="val2"><br> <input type="submit"><br> </form> </body> </html>
pom.xml
<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> <groupId>SpringTesting</groupId> <artifactId>SpringTesting</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <release>21</release> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.3</version> </plugin> </plugins> </build> <dependencies> <!-- Spring Dependency --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.2.0</version> </dependency> <!-- Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.36</version> <scope>provided</scope> </dependency> </dependencies> </project>
项目结构
src └── main └── java └── com └── test ├── Configuration │ ├── AppConfig.java │ ├── AppInitializer.java ├── Controller │ └── FirstController.java └── webapp └── index.jsp
问题原因与解决方案
1. Spring与Tomcat版本不兼容
Spring Web MVC 6.2.0要求Servlet 6.0规范,但Tomcat 9.0.x仅支持Servlet 4.0,版本不匹配会导致Spring容器无法正常初始化,这是核心问题。
- 修复方案:
- 降级Spring Web MVC到5.3.x系列(完全兼容Tomcat 9);
- 升级Tomcat到10.1.x或更高版本(支持Servlet 6.0)。
2. Controller方法返回值错误
addNumbers方法返回void,Spring MVC默认会寻找与方法名同名的视图(如addNumbers.jsp),但项目中不存在该视图,会引发异常。
- 修复方案:
- 若无需返回视图,添加
@ResponseBody注解直接返回响应内容:@RequestMapping("/add") @ResponseBody public String addNumbers(@RequestParam("val1") int val1, @RequestParam("val2") int val2) { System.out.println("Reached"); return "计算结果:" + (val1 + val2); } - 若需要跳转视图,确保项目中存在对应jsp页面并指定返回路径。
- 若无需返回视图,添加
3. 验证容器初始化状态
启动服务器后查看控制台,若未输出AppConfig has been initialized!,说明Spring容器未正常启动,需优先解决版本兼容问题。
4. 表单路径验证
表单action="add"为相对路径,确保访问index.jsp的路径是http://localhost:8080/SpringTesting/index.jsp,提交后会自动拼接为http://localhost:8080/SpringTesting/add,与Controller的@RequestMapping("/add")匹配,此部分逻辑无问题。
内容的提问来源于stack exchange,提问作者Jeet Siyall

