2015-06-30 22 views
2

我使用基於Jquery的Jstree庫捕獲分層數據。數據是JSON格式,我想捕獲並將此數據綁定到我的bean類JstreeJson.java)。這裏是我到目前爲止已經試過..獲取「錯誤不支持的媒體類型」,同時綁定彈簧mvc控制器的JSON數據

Ajax調用:

function getJSON() {  
       var jstree = $('#jstree1').jstree(true).get_json('#', {flat:true}); 
       console.log(JSON.stringify(jstree)); 
       $.ajax({   
        type:"POST", 
        url:"createObjective", 
        data : { jstree: jstree },      
        dataType :"json", 
        success : function(result) { 
         console.log(jstree); 
        console.log(result); 
       }, 
       error: function(jqXHR, textStatus, errorThrown) { 
        console.log(jqXHR); 
        console.log(textStatus, errorThrown); 
       } 
      }); 

     } 

的console.log輸出:

[{"id":"j1_1","text":"Simple root node","icon":true,"li_attr":{"id":"j1_1"},"a_attr":{"href":"#","id":"j1_1_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#"}] 

控制器

@RequestMapping(value="/createObjective",method=RequestMethod.POST) 
public @ResponseBody String createObjective(@RequestBody JstreeJson jstree) 
{ 
    System.out.println(jstree); 
    return "done"; 
} 

Bean類

public class JstreeJson 
{ 
    private String id; 
    private String text; 
    private String parent; 
    // getters and setter 
} 

我曾嘗試加入消耗和接頭,但它沒有對我的輸出

@RequestMapping(value="/createObjective",method=RequestMethod.POST,consumes="application/json",headers = "content-type=application/x-www-form-urlencoded") 
+0

在@RequestMapping上使用'headers =「Accept = application/json」'並移除consumes屬性。 – Darshan

+0

沒有工作得到相同的錯誤 – piechuckerr

回答

0

任何影響試試這個@RequestMapping: -

@RequestMapping(value="/createObjective" method = RequestMethod.POST,consumes= {"application/json;charset=UTF-8"}, produces={"application/json;charset=UTF-8"}) 
public @ResponseBody String createObjective(@RequestBody JstreeJson jstree) 
{ 
    System.out.println(jstree); 
    return "done"; 
} 

或者你可以保持@RequestMapping簡單如下:

@RequestMapping(value="/createObjective") 

Spring會根據請求處理@RequestMapping屬性的其餘部分。

+0

沒有任何變化,仍然得到「無法加載資源:服務器響應狀態415(不支持媒體類型)」 – piechuckerr

+0

添加contentType:「application/json; charset = utf-8」到.ajax和現在我得到:錯誤的錯誤請求 – piechuckerr

+0

400錯誤的請求意味着您的請求正文是郵件形成的。在你的'JsTreeJson'類的頂部添加這個註解,因爲我在你的請求體中看到名爲'icon'的屬性,它不存在於你的bean中。 '@JsonIgnoreProperties(ignoreUnknown = true)'這個註解意味着在反序列化過程中不會包含未知屬性。 –

相關問題