如何为Jackson JSON设置格式化程序?

时间:2023-01-14 18:38:40

with this piece of javascript code I can make a post in Spring MVC with no problem.

使用这段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();
    }
});

Now, the OC Java class has the following structure

现在,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
}

When I try to make a post like this

当我试着像这样做一个帖子

var oc = {
  id: 1,
  date: "2016-04-03",
  type: "1",
  person: 1,
};

The Person formatter is not being called. I know this because if serialize the oc variable and then post it as following:

未调用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();
    }
});

The Person formatter is called with no problem.

调用Person格式化程序没有问题。

How can I set a formatter for Jackson JSON ?

如何为Jackson JSON设置格式化程序?

1 个解决方案

#1


0  

You have to post your nested person object like bellow:

你必须发布你的嵌套人物对象,如下:

var oc = {
  id: 1,
  date: "2016-04-03",
  person: {id:2, name:"name"},
  type: "1",
};

#1


0  

You have to post your nested person object like bellow:

你必须发布你的嵌套人物对象,如下:

var oc = {
  id: 1,
  date: "2016-04-03",
  person: {id:2, name:"name"},
  type: "1",
};