spring4.x引入jackson的maven依赖出现的问题

时间:2021-01-02 18:05:15

在Itellij idea中新建了webapp服务端项目后,引入了springmvc,客户端和服务端传递的数据类型定为json格式。然后在springmvc的配置文件(我的文件名为servlet-context.xml)中添加如下配置:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html; charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html; charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </list>
    </property>
</bean>

注:必须添加在 <mvc:annotation-driven/> 之前。

然后在引入jackson的maven依赖

   <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.5</version>
      <type>bundle</type>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.5</version>
      <type>bundle</type>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.8.0</version>
      <type>bundle</type>
    </dependency>

添加之后,启动服务端。然后就一直报错,下面为错误信息(截取了部分),主要就是jackson初始化所需的类找不到。

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JavaType

org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0' defined in class path resource [servlet-context.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JavaType
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)...
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JavaType
	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
	at java.lang.Class.getDeclaredMethods(Class.java:1975)...
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JavaType
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
	at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
	... 82 more

在网上搜索了各种答案,尝试了各种方法,均不见效,最后我从最开始检查,看看jar包是否导入了,是否成功编译了之类的。最后发现在External Library中成功引入了maven依赖,但是在WEB-INF的libs里面并没有生成jar包(难怪找不到。。。)

spring4.x引入jackson的maven依赖出现的问题

spring4.x引入jackson的maven依赖出现的问题

解决方案:打开了项目的Project Stucture,你会发现lib中缺少jackson的三个jar包,而右侧的Available Elements中有jackson的三个maven依赖。然后进行如下操作:选中lib点击上面的添加按钮“+”(或者右键选中添加),选择添加Library Files,选择jackson的三个依赖,点击“OK”按钮。执行完成后你会发现lib中有了jackson的三个jar包,而右侧的Available Elements中没有依赖了。

spring4.x引入jackson的maven依赖出现的问题

spring4.x引入jackson的maven依赖出现的问题

至此就大功告成了。是不是so easy! 然后我却在这里困了好久,试了各种方法。都去看了jackson的初始化源码。最后问题却出在这儿。所有有时候出现问题的地方可能是你没在意的位置。所有当出现问题时,要顺藤摸瓜,有时候找不到问题所在时就从最初的地方开始检查,也许有意外的收获哟~