zoukankan      html  css  js  c++  java
  • 转转转!!Spring MVC控制器用@ResponseBody声明返回json数据报406的问题

      本打算今天早点下班,结果下午测试调试程序发现一个问题纠结到晚上才解决,现在写一篇博客来总结下。

      是这样的,本人在Spring mvc控制层用到了@ResponseBody标注,以便返回的数据为json格式的数据,到前端JS中用。

      问题来了,当我用火狐调试发现请求总是报:406 Not Acceptable,message:description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()错误。(说是指定的资源已经找到,但它的MIME类型和客户在Accpet头中所指定的不兼容)。

      于是楼主那个纠结,那个郁闷啊。BUG了又BUG,百度了又百度,又怀疑是引用的ext.js文件有问题,因为报ext.base和ext.all语法错误。最后发现是mime类型不匹配。由于我要返回的是json数据,而浏览器接受的是text/html;charset=UTF-8文本类型。不过发现问题还有好几个,一一解决之:

    1. 未在pom文件中引入json的转换包。需要引入如下2个包:
    复制代码
      <!-- jackson start -->
      <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-core-asl</artifactId>
      </dependency>
      <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-mapper-lgpl</artifactId>
       <version>1.9.12</version>
      </dependency>
      <!-- jackson end -->
    复制代码

      2.  spring-servlet.xml文件中需要配置转换的bean

    复制代码
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <util:list id="beanList">
                    <ref bean="mappingJacksonHttpMessageConverter" />
                </util:list>
            </property>
        </bean>
        <bean id="mappingJacksonHttpMessageConverter"
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
    复制代码

      3.  最后,别忘了还要声明util的schema文件和地址,在spring-servlet.xml文件的头部声明部分加入如下三行即可

    xmlns:util="http://www.springframework.org/schema/util"
           http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd

      OK,搞定,大功告成,下班~~~

  • 相关阅读:
    OSPF协议原理
    TCPDUMP抓包方法
    latex:画图
    FrankWolf算法
    Three20 Navigation 迁移到TTTableViewController一直显示加载中的解决办法
    ipa在iTunes中没有图标
    iPhone应用提交AppStore时Application failed codesign verification问题的解决
    XCode4.5 iOS6 SDK 提交AppStore Validate各种错误的解决
    1.forEach():遍历数组,并为每个元素调用传入的函数; 举例:
    CSS实现单行、多行文本溢出显示省略号(…)
  • 原文地址:https://www.cnblogs.com/tenWood/p/7532941.html
Copyright © 2011-2022 走看看