MappingJacksonJsonView not working with Spring 4.x.x

You can see this error line with spring 4.x.x. as:-

Cannot find class [org.springframework.web.servlet.view.json.MappingJacksonJsonView] with Spring 4.2.5.

You may get this error with any version of spring with 4.x.During upgrade of my spring application this issue wasted enough time of mine.Below is bit stack trace of this one issue:-

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping’: Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.web.servlet.view.json.MappingJacksonJsonView] for bean with name ‘xyz’ defined in ServletContext resource [/WEB-INF/xyz-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.view.json.MappingJacksonJsonView…………..

When I verified this issue with some other 4.x release than also this issue was there.The reason of this issue is that “MappingJacksonJsonView” class is deprecated

http://static.javadoc.io/org.springframework/spring-webmvc/4.0.1.RELEASE/deprecated-list.html

and as I was having the version 2.x in my POM for Jackson so above error was coming.

For resolving this issue you will be require to use MappingJackson2JsonView with Jackson 2.x.

I am using my POM entry for Jackson as :-

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

and servlet entry for bean as:-

	<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
		<property name="contentType" value="application/json"/>
	</bean>

And now it’s working perfectly.

Hope this will help some one.

2 comments

  1. I had the same issue with MappingJacksonJsonView. But if I change the dependency to
    com.fasterxml.jackson.core
    jackson-databind
    2.6.5

    Then the tomcat server is getting stopped. What will be the issue?

    Like

Leave a comment