Spring MVC项目启动报SAXParseException JPA命名空间不匹配错误
问题原因分析
核心错误
报错的直接原因是dispatcherServlet-servlet.xml头部的xsi:schemaLocation配置不匹配:你将http://www.springframework.org/schema/data/jpa命名空间对应的XSD地址错误填写为事务模块的http://www.springframework.org/schema/tx/spring-tx-4.3.xsd,XML解析时发现JPA命名空间对应的Schema文件实际目标命名空间为TX,因此抛出校验失败异常。mvn clean package仅执行编译、打包流程,不会触发Spring上下文加载和XML配置校验,所以打包阶段不会暴露该错误,只有应用启动加载Spring容器时才会触发校验抛出异常。
其他潜在配置问题
还有两个隐藏的拼写错误和版本兼容问题需要同步修复:
- 版本不匹配:你使用的Spring版本为5.3.13,但所有XSD都指定了4.3版本,虽然向下兼容但不推荐,建议去掉XSD的版本号让Spring自动匹配当前版本的Schema文件
- 类名拼写错误:
org.springframework.http.converter.xml.MappingJackson2xmlHttpMesssageConverter拼写错误,正确类名为org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter(多写了一个s,Xml首字母需大写)org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean拼写错误,正确类名为org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean
- 缺失TX命名空间Schema配置:你声明了tx命名空间,但之前的schemaLocation里没有配置对应的XSD地址,后续使用tx相关标签时也会报错
修复方案
- 修正头部schemaLocation配置,替换为如下内容:
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
- 将上述两个拼写错误的类名替换为正确类名
修复完成后重新打包部署即可正常启动。
内容的提问来源于stack exchange,提问作者Just Khaithang
相关产品推荐
相关产品推荐

