According to my previous post,
Spring JSON causes problems when the Spring framework is upgraded from 3.0.2 to 3.2.0
I'm trying to get JSON to work in Spring 3.2.0.
Initially, I have tried to configure the disparcher-servlet.xml file as specified here but that could not succeed. It was always causing the following exception.
[CODE]org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name
'org.springframework.web.servlet.view.ContentNegot iatingViewResolver#0' defined in ServletContext resource
[/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is
java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.http.MediaType
After searching a lot about this exception on Google, I came to know that it was reported as a jira issue.
One solution I could find on this post. Accordingly, I have now following XML configuration in my dispatcher-servlet.xml file.
and this configuration resolved the above exception still it could get JSON to work. Google chrome reports the following error as usual.Code:<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="contentNegotiationManager"> <bean class="org.springframework.web.accept.ContentNegotiationManager"> <constructor-arg> <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> <constructor-arg> <map> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> <entry key="html" value="text/html"/> <entry key="atom" value="application/atom"/> </map> </constructor-arg> </bean> </constructor-arg> </bean> </property> <property name="defaultViews"> <list> <!-- JSON View --> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> <!-- XML View --> <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="packagesToScan"> <list> <value>documentLoader.domain</value> </list> </property> </bean> </constructor-arg> </bean> </list> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
NOTE: I have Jackson 1.9.8 (its download page) library on the classpath. With Jackson 2.1.1, it didn't work throwing the following exception at runtime.Failed to load resource: the server responded with a status of 406 (Not Acceptable)
Because Jackson 2.1.1 has the class ObjectMapper in another package com.fasterxml.jackson.databind.ObjectMapper. What is the way?Code: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
I'm now quite unsure as to how to make JSON respond correctly in Spring 3.2.0. If someone has found the solution then please let me know.
The following is my entire dispatcher-servlet.xml file, if it needs to see.
Code:<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="controller" /> <context:component-scan base-package="validatorbeans" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="contentNegotiationManager"> <bean class="org.springframework.web.accept.ContentNegotiationManager"> <constructor-arg> <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> <constructor-arg> <map> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> <entry key="html" value="text/html"/> <entry key="atom" value="application/atom+xml"/> </map> </constructor-arg> </bean> </constructor-arg> </bean> </property> <property name="defaultViews"> <list> <!-- JSON View --> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> <!-- XML View --> <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="packagesToScan"> <list> <value>documentLoader.domain</value> </list> </property> </bean> </constructor-arg> </bean> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> </beans>


Reply With Quote