在JavaWEB项目中,使用SSM框架,在项目启动时出现如下错误:
Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] for bean with name ‘mappingJacksonHttpMessageConverter’ defined in class path resource [spring-mvc.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
查看之后,出错的地方为配置文件中配置了一个bean,id为mappingJacksonHttpMessageConverter,class为
Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] for bean with name ‘mappingJacksonHttpMessageConverter’ defined in class path resource [spring-mvc.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
查看之后,出错的地方为配置文件中配置了一个bean,id为mappingJacksonHttpMessageConverter,class为
“org.springframework.http.converter.json.MappingJacksonHttpMessageConverter”,
纠结很久之后,发现其实就是和所用的的Spring框架的版本有关,这个要注意spring版本,3.x和4.x配置不同
解决办法:
spring3.x是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
spring4.x是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <!-- 支持JSON数据格式 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!-- 解析导出文件byte流 --> <ref bean="fastJsonHttpMessageConverter" /> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> </bean> <!-- 使用fastJson来支持JSON数据格式 --> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <value>WriteMapNullValue</value> <value>QuoteFieldNames</value> </list> </property> </bean>