springmvc 請求數據時,報406錯誤????

今天分享一個知識,

最近在自學springmvc 遇到的一件特別惱人的事兒, 寫好的demo 在請求網絡數據時總是失敗返回406錯誤,修改好了406錯誤,無緣無故有給我返回500, 網上找了很多資料也沒能解決我的問題,就開始代碼亂拼,結果你懂得,突然就能把對象返回成json數據給客戶端了.小小高興了n小時,然後靜下心來好好的對比了一下代碼,發現和以前的代碼沒有什麼不同,唯一的只是在配置上出了點問題,


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">


    <context:component-scan base-package="net.controller">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>




    <mvc:annotation-driven />   (這段代碼就是不同的地方)


    <!--配置一個viewResolver來處理返回的頁面,配置前置和後置 -->
    <bean id="viewresolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>


我專門搜了一下這段代碼的功能,於是在這裏 http://kingliu.iteye.com/blog/1972973

找到了一句話 " 當我們需要controller返回一個map的json對象時,可以設定<mvc:annotation-driven /> ",我專門的試了一下去掉了這句話,果然出現了406的錯誤.搞死我了,當然這可能只是出現406的一個可能的原因,我遇到了所以分享給大家,希望大家少走彎路.

附帶我的demo地址: [email protected]:Marshallee/SpringMVC_JSON.git

發佈了20 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章