使用Spring MVC出现资源找不到错误,疑版本不匹配求适配版本
问题与解决方案
问题描述
开发首个网站应用时出现404错误,错误提示:
The origin server did not find a current representation for the target resource or is not willing to divulge that one exists
怀疑是版本不匹配导致,寻求适配版本及解决办法,相关代码与配置如下:
相关代码与配置
Controller代码
@Controller @RequestMapping("/") public class HomeController { @GetMapping public String Default() { return "Home"; } }
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Phone-Accessories</display-name> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/PhoneAccessories-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
servlet.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <context:component-scan base-package="com.dth.Controller"></context:component-scan> <import resource="classpath:IoC.xml" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> <mvc:annotation-driven enable-matrix-variables="true"/> </beans>
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>com.dth</groupId> <artifactId>PhoneAccessories</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- Spring WebMVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.0.3</version> </dependency> <!-- JUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> </plugin> </plugins> <finalName>project name</finalName> </build> </project>
部署配置截图

错误结果截图

核心问题与适配方案
版本不匹配分析
当前配置存在严重的版本兼容问题:
- Spring 6.x(spring-webmvc 6.0.3)基于Jakarta EE 9+,要求使用
jakarta.servlet-api而非旧的javax.servlet-api,且Java版本必须≥17,Servlet容器(如Tomcat)需≥10.x。 - 你的web.xml是Servlet 3.0版本,依赖的是
javax.servlet-api 4.0.1,完全不兼容Spring 6.x。 - servlet.xml中引用的Spring schema是4.3版本,与Spring 6.x不匹配。
两种适配方案
方案一:降级到Spring 5.x(兼容Java 8+,入门友好)
Spring 5.x支持Java 8+,兼容Servlet 3.0+,无需大幅调整现有配置:
- 修改pom.xml中的spring-webmvc版本:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.29</version> <!-- Spring 5.x最后一个稳定版本 --> </dependency>
- 更新servlet.xml中的schema版本为5.3:
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-5.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-5.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-5.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-5.3.xsd"
- 保持
javax.servlet-api 4.0.1和web.xml 3.0版本不变,使用Tomcat 9.x即可运行。
方案二:升级到Spring 6.x的正确配置
如果要使用最新的Spring 6.x,需做以下调整:
- 升级Java版本到17+。
- 修改pom.xml依赖:
<!-- 替换javax.servlet-api为jakarta.servlet-api --> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> <!-- 保持spring-webmvc 6.0.3或升级到最新稳定版 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.0.15</version> <!-- 最新稳定版 --> </dependency>
- 更新web.xml为Servlet 5.0版本(适配Jakarta EE 9):
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> <!-- 原有内容不变 --> </web-app>
- 更新servlet.xml中的schema版本为6.0:
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-6.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-6.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-6.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-6.0.xsd"
- 使用Tomcat 10.x或更高版本的Servlet容器。
额外检查项
- 确认
/WEB-INF/views/Home.jsp文件存在,文件名大小写与Controller返回的"Home"一致(JSP文件名区分大小写)。 - 检查Controller的包路径:Java包名通常为小写,
com.dth.Controller建议改为com.dth.controller,同时调整servlet.xml中的context:component-scan的base-package为com.dth.controller。 - 确认Tomcat部署的上下文路径正确(截图中部署路径为
/PhoneAccessories,访问时需用http://localhost:8080/PhoneAccessories而非根路径)。
内容的提问来源于stack exchange,提问作者do hai
相关产品推荐
相关产品推荐

