• 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 />这行一定不可以缺少,否则也会返回失败

  • 相关阅读:
    畅通工程续 (dijkstra)
    最短路径问题 HDU 3790
    【基础算法-模拟-例题-玩具谜题】-C++
    【基础算法-模拟-例题-金币】-C++
    【动态规划例题-数塔问题】-C++
    【基本数据结构之'图'】
    【最小生成树之Kruskal例题-建设电力系统】-C++
    【最短路算法例题-升降梯上】-C++
    【基本数据结构之栈】
    【栈-例题】网页跳转-C++
  • 原文地址:https://www.cnblogs.com/stevenx1987/p/4165991.html
Copyright © 2020-2023  润新知