将 Jackson 添加到 POM 会导致 JsonProcessingException 的 NoClassDefError

Adding Jackson to POM causes NoClassDefError for JsonProcessingException

首先,很抱歉再次提出这个问题。上一次是 3 个月前,我找到了一种权宜之计,当时这种方法适用于概念验证。不幸的是,我真的需要让它在整个过程中正常工作

作为我当前项目的一部分,我需要能够在向 Spring 服务器发送请求和从 JSON 服务器发送请求时自动序列化对象。有人告诉我为此使用杰克逊。问题是,每次我将 Jackson 添加到 POM 中时,它都会构建,但 servlet 在找不到 com.fasterxml.jackson.core.JsonProcessingException 时会抛出 java.lang.NoClassDefError。不过,我已经检查过,JsonProcessingException 是 WAR.

中应该出现的地方

我怀疑这导致 Spring 尝试为 servlet 自动装配 Jackson 解析器,但它无法找到所有依赖项。我不知道为什么会这样,因为它们 应该 被 Maven 正确引入。

我的 pom.xml 概括了公司的具体情况:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.company.department</groupId>
        <artifactId>project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>project-restservice</artifactId>

    <packaging>war</packaging>
    <name>projectMaven Restservice</name>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>project-common</artifactId>
            <version>1.0-SNAPSHOT</version>

        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>project-processor</artifactId>
            <version>1.0-SNAPSHOT</version>

        </dependency>

        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>project-ui-service</artifactId>
            <version>1.0-SNAPSHOT</version>

        </dependency>
        <dependency>
            <groupId>com.qualcomm.qes.logging</groupId>
            <artifactId>project-logger</artifactId>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>org.slf4j</artifactId>
                    <groupId>com.springsource.slf4j.api</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>org.slf4j</artifactId>
                    <groupId>com.springsource.slf4j.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.qualcomm.qes.sso.downstream</groupId>
            <artifactId>sso-downstream-auth</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>bcprov-jdk15on</artifactId>
                    <groupId>bouncycastle</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- SpringSource -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.web.servlet</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.beans</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.oxm</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>xml-apis</artifactId>
                    <groupId>xml-apis</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Other -->


        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.1</version>
        </dependency>

        <!-- JMS -->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>com.springsource.javax.transaction</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>com.springsource.javax.servlet</artifactId>
            <scope>provided</scope>
        </dependency>


        <!-- TIBCO JMS -->
        <dependency>
            <groupId>com.tibco</groupId>
            <artifactId>tibjms</artifactId>

        </dependency>

        <!-- TESTS -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <!-- TODO: wants version number -->
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <filtering>true</filtering>
                            <targetPath>WEB-INF</targetPath>
                            <includes>
                                <include>**/jboss-classloading.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>deploy-war</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>deploy-to-server</id>
                                <phase>install</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <copy file="${project.build.directory}/${project.artifactId}-${project.version}.war" todir="${project.jboss.home}/standalone/deployments/project.ear/"></copy>
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>


    </profiles>


</project>

我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/project-restservice-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>project-restservice</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>project-restservice</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

我的 servlet 上下文:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.company.department.project.controller" />

    <mvc:annotation-driven/>

    <mvc:resources mapping="/html/**" location="/static/html/" />
    <mvc:resources mapping="/js/**" location="/static/js/" />
    <mvc:resources mapping="/css/**" location="/static/css/" />
    <mvc:resources mapping="/images/**" location="/static/images/" />


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>     
</bean>

我的控制器:

@Controller
public class RestServiceWebController extends BaseRestServiceController
{

   @Autowired
   private SampletableService sampletableService;

   @RequestMapping(value="/sample", method=RequestMethod.GET)
   public String customerPortal(ModelMap model)
   {
     return "sample";
   }

  @RequestMapping(value="/testService", method=RequestMethod.GET)
  public @ResponseBody Map<String, String> testMethod()
  {
    logger.info("In testService");
    Map<String,String> testResponse = new HashMap<String, String>();
    testResponse.put("1", "2");
    testResponse.put("3", "4");
    testResponse.put("5", "6");
    testResponse.put("7", "8");
    testResponse.put("9", "0");
    return testResponse;
  }
}

日志摘录:

2015-02-19 09:35:56,638 ERROR [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 54) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3339)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3777)
    at org.jboss.as.web.deployment.WebDeploymentService.doStart(WebDeploymentService.java:156)
    at org.jboss.as.web.deployment.WebDeploymentService.access[=14=]0(WebDeploymentService.java:60)
    at org.jboss.as.web.deployment.WebDeploymentService.run(WebDeploymentService.java:93)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
    at org.jboss.threads.JBossThread.run(JBossThread.java:122)
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2493)
    at java.lang.Class.getDeclaredConstructors(Class.java:1901)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:230)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:972)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:945)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:126)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 22 more

如果您需要更多信息,请告诉我。

编辑:

所以进行了一些探索和跟踪类似的线索,我设法将其追踪到自动装配 Jackson bean 的标签。删除它并像这样手动添加转换器:

<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

结果相同的错误。所以至少现在我知道是什么原因造成的,但是依赖项 所以我不确定为什么它说找不到它们。

你错过了Jackson-core.jar。将其添加到您的 pom

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.1</version>
</dependency>