Adding Jackson to POM causes NoClassDefError for JsonProcessingException

10,252

You miss the Jackson-core.jar. Add it to your pom

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.1</version>
</dependency>
Share:
10,252
Grendus
Author by

Grendus

Updated on June 05, 2022

Comments

  • Grendus
    Grendus almost 2 years

    First off, I apologize for bringing this question up again. The last time was 3 months ago, and I found a stopgap method that kind of worked back then for the proof of concept. Unfortunately, I really need to get this working properly for the full

    As part of my current project, I need to be able to serialize objects automatically to and from JSON as requests are sent to and from a Spring server. I've been told to use Jackson for this. The problem is, every time I add Jackson to the POM it will build, but the servlet throws a java.lang.NoClassDefError when it can't find com.fasterxml.jackson.core.JsonProcessingException. I've checked though, JsonProcessingException is where it should be expected in the WAR.

    My suspicion is that the is causing Spring to try to autowire a Jackson parser for the servlet and it's unable to find all of the dependencies. I have no idea why this is though, as they should be getting pulled in properly by Maven.

    My pom.xml with company specifics generalized:

    <?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>
    

    My 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>
    

    My servlet context:

    <?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>
    

    My controller:

    @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;
      }
    }
    

    Excerpt from the log:

    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$1.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$000(WebDeploymentService.java:60)
        at org.jboss.as.web.deployment.WebDeploymentService$1.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
    

    Please let me know if you need any more information.

    Edit:

    So a little bit of probing and following similar threads, and I managed to trace it to the tag which is autowiring the Jackson bean. Removing that and manually adding a converter like so:

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

    Results in the same error. So at least now I know what's causing it, but the dependencies are there so I'm not sure why it's saying it can't find them.

  • Grendus
    Grendus about 9 years
    Added it, ran, and got the same error.
  • Jens
    Jens about 9 years
    @Grendus excectly the same?
  • Grendus
    Grendus about 9 years
    Exactly the same: Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.Reque‌​stMappingHandlerAdap‌​ter#0': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
  • Jens
    Jens about 9 years
    @Grendus try a clean and rebuild.
  • Grendus
    Grendus about 9 years
    mvn clean install -Ddeploy=windows -DskipTests=true is how I usually build the project. I believe that would be a clean and rebuild every time. Should I be doing something else?
  • Jens
    Jens about 9 years
    @Grendus No that is ok. But the class is in 'jackson-core.jar'
  • Grendus
    Grendus about 9 years
    It is in jackson-core-2.5.1.jar, which is now being explicitly imported.
  • Grendus
    Grendus about 9 years
    After some more poking around, I found that Maven wasn't putting the dependencies in the correct location for the server. Adding the <scope>compiled</scope> tag to the pom fixed the problem. Thanks for all the help.
  • juckele
    juckele over 7 years
    You mean <scope>compile</scope>?
  • Berlin Brown
    Berlin Brown almost 7 years
    I had the same error, anyone figure this out.
  • Jens
    Jens almost 7 years
    @BerlinBrown Which error?