使用这段javascript代码,我可以在Spring MVC上发帖,没有任何问题。
var oc = {
id: 1,
date: "2016-04-03",
type: "1",
};
$.ajax({
url: "addOC.do",
type: "POST",
contentType: 'application/json',
data: JSON.stringify(oc),
dataType:"json",
success: function(object){
alert();
}
});
现在,OC Java类具有以下结构
public class OC{
private Integer id;
private Date date;
private Person person ;
private String type;
// getters and setters
}
public class Person{
private Integer id;
private String name;
// getters and setters
}
当我尝试发布这样的帖子时
var oc = {
id: 1,
date: "2016-04-03",
type: "1",
person: 1,
};
未调用Person格式化程序。我知道这是因为如果序列化oc变量然后将其发布如下:
var form = "id=1&date="2016-04-03"&type="1"&person=1";
$.ajax({
url: "addOC.do",
type: "POST",
data: form,
dataType:"json",
success: function(object){
alert();
}
});
调用Person格式化程序没有问题。
如何设置Jackson JSON的格式化程序?
答案 0 :(得分:0)
你必须发布你的嵌套人物对象,如下:
var oc = {
id: 1,
date: "2016-04-03",
person: {id:2, name:"name"},
type: "1",
};