首页 > 代码库 > Spring MVC 的json问题解决(406 Not Acceptable)

Spring MVC 的json问题解决(406 Not Acceptable)

spring 版本:spring-framework-3.2.12.RELEASE

需要额外jar包:jackson-mapper-asl-1.9.7.jar和jackson-core-asl-1.9.7.jar

 

spring-servlet.xml配置

<?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: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/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd        http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        ">    <mvc:annotation-driven />    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->    <bean id="mappingJacksonHttpMessageConverter"        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">        <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>    <context:component-scan base-package="com.controllers" />    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/json/" />        <property name="suffix" value=".jsp" />    </bean></beans>

其中 <mvc:annotation-driven />这行一定不可以缺少,否则也会返回失败

 

Spring MVC 的json问题解决(406 Not Acceptable)