一、异常描述
项目使用spring-webmvc作为MVC框架,但是项目启动时报出“[ERROR] DispatcherServlet: 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..Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException..Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException..com/fasterxml/jackson/databind/ObjectMapper..”异常,具体日志如下
[18:03:56] [ERROR] DispatcherServlet: Context initialization failed@b@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@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)@b@ at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)@b@ at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)@b@ at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)@b@ at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)@b@ at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)@b@ at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)@b@ at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)@b@ at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)@b@ at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)@b@ at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)@b@ at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)@b@ at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)@b@ at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)@b@ at javax.servlet.GenericServlet.init(GenericServlet.java:212)@b@ at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)@b@ at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)@b@ at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4350)@b@ at org.apache.catalina.core.StandardContext.start(StandardContext.java:4659)@b@ at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)@b@ at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)@b@ at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)@b@ at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)@b@ at org.apache.catalina.startup.Embedded.start(Embedded.java:825)@b@ at org.codehaus.mojo.tomcat.AbstractRunMojo.startContainer(AbstractRunMojo.java:558)@b@ at org.codehaus.mojo.tomcat.AbstractRunMojo.execute(AbstractRunMojo.java:255)@b@ at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:105)@b@ at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:577)@b@ at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:324)@b@ at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:247)@b@ at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:104)@b@ at org.apache.maven.cli.MavenCli.execute(MavenCli.java:427)@b@ at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:157)@b@ at org.apache.maven.cli.MavenCli.main(MavenCli.java:121)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)@b@ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)@b@ at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)@b@ at java.lang.reflect.Method.invoke(Method.java:597)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)@b@ at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)@b@Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException@b@ at java.lang.Class.getDeclaredConstructors0(Native Method)@b@ at java.lang.Class.privateGetDeclaredConstructors(Class.java:2398)@b@ at java.lang.Class.getDeclaredConstructors(Class.java:1838)@b@ at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:230)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:972)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:945)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)@b@ at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271)@b@ at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:121)@b@ at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)@b@ at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)@b@ ... 42 more@b@Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException@b@ at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)@b@ at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)@b@ ... 57 more@b@Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#0' defined in ServletContext resource [/WEB-INF/spring-mvc.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)@b@ at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271)@b@ ... 48 more@b@Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper@b@ at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)@b@ at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)@b@ at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000)@b@ ... 52 more@b@Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper@b@ at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.<init>(MappingJackson2HttpMessageConverter.java:62)@b@ at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)@b@ at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)@b@ at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)@b@ at java.lang.reflect.Constructor.newInstance(Constructor.java:513)@b@ at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)@b@ ... 54 more@b@Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper@b@ at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)@b@ at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)@b@ ... 60 more
二、解决方法
1.在maven项目的POM文件中添加依赖包,如下
<dependency>@b@ <groupId>com.fasterxml.jackson.core</groupId>@b@ <artifactId>jackson-core</artifactId>@b@ <version>2.5.0</version>@b@</dependency>@b@ @b@<dependency>@b@ <groupId>com.fasterxml.jackson.core</groupId>@b@ <artifactId>jackson-databind</artifactId>@b@ <version>2.5.0</version>@b@</dependency>