zoukankan      html  css  js  c++  java
  • 前端控制台返回406错误解决方法

    问题描述:

     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头中所指定的不兼容)。

       前后台代码经过排查都没有问题,本人在Spring mvc控制层用到了@ResponseBody标注,以便返回的数据为json格式的数据,到前端JS中用。而前端却需要text/html;charset=UTF-8这样的格式,导致的问题。

    解决办法如下:

    1.需要使用jackson,在pom.xml中引入jackson的依赖

    <!-- 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.spring-servlet.xml文件的头部声明部分加入如下三行即可(因为util:list)

    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

     

     

  • 相关阅读:
    论文阅记 YOLOv4: Optimal Speed and Accuracy of Object Detection
    【项目实战】yolov3-tiny人脸数据模型训练
    面试题54. 二叉搜索树的第k大节点
    102. 二叉树的层序遍历
    107. 二叉树的层次遍历 II
    连续子数组的最大和
    172. 阶乘后的零
    26 进制
    405. 数字转换为十六进制数
    504. 七进制数
  • 原文地址:https://www.cnblogs.com/sllcom/p/9096914.html
Copyright © 2011-2022 走看看