You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring5.3.13项目启动找不到Jackson2ObjMapperFactoryBean类如何解决

问题解决方案

错误1:Jackson相关类找不到

  • 核心原因:类名拼写错误
    你配置中写的org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean是错误拼写,正确完整类名是org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean,漏写了Object中的ect后缀。
    另外XML消息转换器的类名也存在拼写错误:你写的org.springframework.http.converter.xml.MappingJackson2xmlHttpMesssageConverter需要修正为org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter,首字母X要大写,Messsage多了一个s。
  • 修正后的配置段:
<bean id="jsonMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
    <property name="simpleDateFormat" value="yyyy-MM-dd HH:mm:ss" />
</bean>
<bean id="xmlMapper" parent="jsonMapper">
    <property name="createXmlMapper" value="true"/>
</bean>
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="jsonMapper"></property>
        </bean>
        <bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
        <property name="objectMapper" ref="xmlMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

错误2:persistence单元无法解析

  • 核心原因有三点:
    1. persistence.xml头部命名空间拼写错误:xmlns:xsi="http://www.w3.org/2001/XMLSchema-isntance"中isntance是拼写错误,正确为instance
    2. LocalContainerEntityManagerFactoryBean配置未指定持久化单元名称,你定义的持久化单元名为toDo,需要在entityManagerFactory的配置中增加对应属性
    3. dispatcherServlet-servlet.xml中的JPA schema地址拼写错误:http://www.springframework.org/schema/data/jpa/sprin-jpa-1.8.xsd漏写了g,应为spring-jpa,且Spring 5.3版本建议使用无版本号的schema地址,避免版本不匹配问题
  • 修正方案:
    首先修正persistence.xml的头部和内容:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_2.xsd" version="2.2">
<persistence-unit name="toDo">
    <description>My Persistence Unit</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
    </properties>
</persistence-unit>
</persistence>

然后修正entityManagerFactory的配置,指定实体类扫描路径和持久化单元名:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="persistenceUnitName" value="toDo" />
    <property name="packagesToScan" value="org.charlie.entity" />
</bean>

最后修正dispatcherServlet-servlet.xml的schema地址,将所有带版本号的schema改为无版本号,比如JPA的schema修正为http://www.springframework.org/schema/data/jpa/spring-jpa.xsd即可。

额外检查项

  • 确保persistence.xml放在src/main/resources/META-INF目录下,编译后会出现在classes/META-INF路径下,Spring才能正确扫描到
  • 确保项目依赖中已经引入了spring-web、jackson-databind、spring-orm、hibernate-core、h2数据库等必要依赖,没有缺失

内容的提问来源于stack exchange,提问作者Just Khaithang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 03:54:08