1.现象2.解决方法
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
方法一:spring配置添加
<!-- 注解驱动 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
<property name="features">
<list>
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
方法二、
spring配置添加
<!-- 避免IE执行AJAX时,返回JSON出现下载文件
spring3为:org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
spring4为:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
-->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>
注意:
如果遇到下列报错
[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/DefaultIndente
可能原因:1.jar包版本冲突,2.缺少依赖,
可以看到我们的依赖已经添加了,这里原因是
spring-4.1.3.RELEASE 升级为 spring-4.3.7.RELEASE
原来的jackson-2.4.2就会出现版本冲突,然后系统就会报java.lang.NoClassDefFoundError,必须改为jackson-2.8.7,