我試圖找出一種方法來通過json與spring mvc 3.1傳遞一個複雜的對象。我也使用knockoutjs,所以請把ko.toJSON等同於JSON.stringify。有沒有辦法使用spring mvc 3.1將json反序列化成複雜對象?
DeployT
這裏是AJAX調用:
$.ajax({
url: "/doAction",
type: "post",
data: ko.toJSON({"complexObjectA": ko.toJSON(self.complexObjectA()), "complexObjectB": ko.toJSON(self.complexObjectB()), "id": "", "text": ""}),
dataType: "json",
contentType: "application/json; charset=utf-8",
// callback handler that will be called on success
success: function (response, textStatus, jqXHR) {
//dosomething
},
// callback handler that will be called on error
error: function (jqXHR, textStatus, errorThrown) {
// log the error to the console
},
// callback handler that will be called on completion
// which means, either on success or error
complete: function() {
//dosomething
}
});
彈簧控制器的代碼如下所示:
@RequestMapping(value = "/doAction", method = RequestMethod.POST)
@ResponseBody
public String doAction(@RequestBody MyForm form, HttpServletRequest request, HttpServletResponse response) {
和MyForm的被定義爲這樣:
public class MyForm {
private ComplexObjectA complexObjectA;
private ComplexObjectB complexObjectA;
private String id;
private String text;
與approp讓公共getter/setter。
當我嘗試進行此調用時,出現錯誤400 客戶端發送的請求在語法上不正確()。
複雜的對象都是通過json get獲得的,並且它們從對象到json和js對象都很好地被序列化。
我是否需要用Jackson創建一個特殊的解串器才能做到這一點?