我正在尝试找出一种通过 json 和 spring mvc 3.1 传递复杂对象的方法。我也在使用knockoutjs,所以把ko.toJSON 等同于JSON.stringify。
部署T
这是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;
使用适当的公共 getter/setter。
当我尝试进行此调用时,我收到错误 400 客户端发送的请求在语法上不正确 ()。
复杂的对象都是通过更早的 json get 获得的,它们可以很好地从对象序列化到 json 和 js 对象。
为了做到这一点,我是否需要用 Jackson 创建一个特殊的反序列化器?