JSON_springMVC下,使用AJAX請求,產生406錯誤的解決手記

          首先,spring相關依賴如下:
           1 <properties>
           2     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           3     <spring.version>4.1.0.RELEASE</spring.version>
           4 </properties>
           5 
           6 <dependency>
           7     <groupId>org.springframework</groupId>
           8     <artifactId>spring-webmvc</artifactId>
           9     <version>${spring.version}</version>
          10 </dependency>
          11 <dependency>
          12     <groupId>org.springframework</groupId>
          13     <artifactId>spring-web</artifactId>
          14     <version>${spring.version}</version>
          15 </dependency>
          使用springMVC創建一個可以返回JSON的方法,需要如下書寫,關鍵要為特定的方法添加@ResponseBody,將返回值自動轉換為JSON格式,作為相應的內容
          1     @RequestMapping("/subCates")
          2     public @ResponseBody List<Category> listSubCates(@Param Integer pId){
          3         List<Category> cs = categoryService.listCategoryByPcid(pId);
          4         return cs;
          5     }
          該注解需要在xxx-servlet.xml配置中開啟
          1 <mvc:annotation-driven />
          單是如此還不夠,經過調查,springMVC還依賴了處理JSON的類庫Jackson
           1 <!-- JSON -->
           2 <dependency>
           3     <groupId>com.fasterxml.jackson.core</groupId>
           4     <artifactId>jackson-core</artifactId>
           5     <version>2.5.0</version>
           6 </dependency>
           7 
           8 <dependency>
           9      <groupId>com.fasterxml.jackson.core</groupId>
          10     <artifactId>jackson-databind</artifactId>
          11     <version>2.5.0</version>
          12 </dependency>
          13 
          14 <dependency>
          15      <groupId>org.codehaus.jackson</groupId>
          16      <artifactId>jackson-mapper-asl</artifactId>
          17      <version>1.9.13</version>
          18 </dependency>
          本人最開始只依賴了mapper和core,沒有添加databind依賴才導致了406錯誤。調查之后發現,其實只需要添加databind依賴就可以解決問題,databind依賴了core等配置。

          posted on 2015-01-27 16:52 都較瘦 閱讀(2269) 評論(0)  編輯  收藏 所屬分類: JSON相關問題積累


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2015年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 普定县| 桐乡市| 玉屏| 宣威市| 临高县| 竹溪县| 辽宁省| 宁强县| 肇东市| 内江市| 孟村| 芜湖县| 社旗县| 巴楚县| 秦皇岛市| 常熟市| 红安县| 滕州市| 蓬莱市| 三门县| 行唐县| 延边| 塔城市| 夏河县| 井研县| 崇明县| 和硕县| 莱西市| 梅河口市| 兴安盟| 湄潭县| 和林格尔县| 尚志市| 古蔺县| 平顺县| 京山县| 米林县| 藁城市| 长丰县| 邳州市| 柳林县|