10

I have been doing a poc on Spring4 and RestWS integration. I am new to both with barely a weeks exposure. I am following instructions from blogs. I understand that in Spring 4 and restWS setup jackson-core/anotation/databind 2 is to be used for correct message converstion of JSON <-> Java object to happen. Also org.springframework.http.converter.json.MappingJackson2HttpMessageConverter is to be used for message conversion. I satisfied all these requirements. However when I try to start application, I get bean create exception. stack

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'regionController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.View com.leggmason.lmpamfs.controllers.RegionController.jsonView_i; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.view.json.MappingJacksonJsonView#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.view.json.MappingJacksonJsonView]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.servlet.View com.leggmason.lmpamfs.controllers.RegionController.jsonView_i; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.view.json.MappingJacksonJsonView#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.view.json.MappingJacksonJsonView]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.view.json.MappingJacksonJsonView#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.view.json.MappingJacksonJsonView]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 24 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.servlet.view.json.MappingJacksonJsonView]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
    ... 35 more
Caused by: java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper
    at org.springframework.web.servlet.view.json.MappingJacksonJsonView.(MappingJacksonJsonView.java:65)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 37 more
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.map.ObjectMapper
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    ... 43 more

It seems that something is wrong with MappingJacksonJsonView class. Could following from factory file be the cause of trouble ?

 <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">  
       <property name="contentType" value="text/plain"/>  
  </bean>  

Am I supposed to replace this class as well? I will help assistance to fix this.

Thanks, Suvojit

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
suvojit
  • 311
  • 1
  • 6
  • 18
  • Is there a reason you're not letting Spring Boot do all this for you? – chrylis -cautiouslyoptimistic- May 13 '14 at 13:01
  • I am sorry, I am not aware of SpringBoot. As I said I am a newbie. I just followed the online tutorials available to get started. – suvojit May 13 '14 at 13:05
  • Try using Spring Boot instead. It takes care of most of the usual setup, like JSON mapping, so you can get started with writing controllers instead of setup. – chrylis -cautiouslyoptimistic- May 13 '14 at 13:09
  • Ah, I note that you're using the old (Jackson 1) mapper class, so it may be that you have a version mismatch. – chrylis -cautiouslyoptimistic- May 13 '14 at 14:01
  • Make sure you have jackson1 and not jackson2 on your classpath with your current configuration. If you have a `fasterxml` dependency somewhere you have jackson2 and not jackson1. Either change the dependency or use the `MappingJackson2JsonView`. – M. Deinum May 13 '14 at 14:46
  • @Chrylis: I am not using Jackson 1 api's. jars in my lib are, jackson-core-2.0.2, jackson-annotation-2.3.0, jackson-bindings 2.3.0. – suvojit May 14 '14 at 06:05
  • @Chrylis: Apart from this there are jars from Spring 4 and few jars related to jdbc, logging and apache wicket. I understand that it is definitely a version mismatch issue but I fail to pin point the mismatch. – suvojit May 14 '14 at 06:20
  • @M.Deinum : As I mentioned there are no jackson 1.x api jars in my classpath. Can you please elaborate on the fix you proposed. My source of knowledge is : http://xpadro.blogspot.com/2014/01/migrating-spring-mvc-restful-web.html and http://www.programming-free.com/2014/03/spring-mvc-40-restful-web-service-json.html – suvojit May 14 '14 at 06:20
  • 2
    Check my comment. You are using the jackson1 class `MappingJacksonJsonView` but should use `MappingJackson2JsonView`. – M. Deinum May 14 '14 at 06:23
  • @M.Deinum : I misunderstood your suggestion. It worked nicely for me. Thank you so much. – suvojit May 14 '14 at 06:42
  • @user3359005 Added an answer, please accept and/or upvote. – M. Deinum May 14 '14 at 07:00

2 Answers2

25

You are currently using the MappingJacksonJsonView which is designed to work with Jackson1 .

You mention the following:

I am not using Jackson 1 api's. jars in my lib are, jackson-core-2.0.2, jackson-annotation-2.3.0, jackson-bindings 2.3.0.

Which means you have Jackson2 on your classpath. Jackson1 and Jackson2 aren't compatible (different packages, different classes etc.).

You have 2 possible solutions

  1. Don't use Jackson2 and switch your dependencies to Jackson1
  2. Replace the MappingJacksonJsonView with the MappingJackson2JsonView
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Thanks for this answer, I had updated the XML to use org.springframework.http.converter.json.MappingJackson2HttpMessageConverter but neglected to use org.springframework.web.servlet.view.json.MappingJackson2JsonView as well in the dynamic output controllers. – ledlogic May 25 '18 at 03:06
1

For Spring4 Rest + Json POC The following App Context is enough

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="org.name.controller" />
    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />
    <mvc:resources location="/statics/" mapping="/statics/**"/>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Make sure you have dependency for Jackson (e.g. in maven)

<!-- jackson so spring mvc will handle json responses out of the box -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.0</version>
</dependency>

You do not specifically need to define MappingJackson2JsonView; MappingJackson2HttpMessageConverter/MappingJacksonHttpMessageConverter will be created automatically when jackson is the the class path (the default is only when ContentType/Accept are application/json).

Jackson1 is deprecated, but still supported if from any reason you are limited to Jackson1 See Spring 4 docs

Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • Please have a look at : http://xpadro.blogspot.com/2014/01/migrating-spring-mvc-restful-web.html It says that jackson 2.x is to be used with Spring 4. Did you get to run your poc app with Jackson 1.x and Spring4 ? – suvojit May 14 '14 at 06:24
  • If I want it to use the JSON resolver for all requests, not just the `application/json` ones, then what should I do? – Vihung May 13 '16 at 17:17
  • You can customize your configurations via the setting supportedMediaTypes See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/AbstractHttpMessageConverter.html#setSupportedMediaTypes-java.util.List- http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html – Haim Raman May 15 '16 at 03:32