• 前端控制台返回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

     

     

  • 相关阅读:
    数据库之事务与常见故障
    数学的魅力 之 正多边形
    html5 的基础理解1
    android 引入开源项目
    android 图片查看器
    java 线程安全
    python3 自动生成requirement.txt
    centos 7 安装 python3.7
    python3 创建,激活虚拟环境
    Mac 配置poetry
  • 原文地址:https://www.cnblogs.com/sllcom/p/9096914.html
Copyright © 2020-2023  润新知