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

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM