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

 

 

 

 

posted @ 2018-05-27 18:51  向上的栗子  阅读(3073)  评论(0编辑  收藏  举报