让我们举个例子。我有一个 pojo 类,如下所示。
public class MyRecord{
private String name;
private String id;
//constructors and getters,setters
}
当我得到上面的 toJson(new MyRecord("MyName","myId") 输出时,我可以获得。
{
"name": "MyName",
"id": "123"
}
我继承了一个如下的方法来添加日期时间。
public class MyRecordWithDateTime extends MyRecord{
private String DateTime;
//constructors and getters,setters
}
所以当我调用 toJson(new MyRecordWithDateTime("2016-01-01", "MyName", "myId"))
输出是这样的
{
"name": "MyName",
"id": "123",
"dateTime": "2016-01-01"
}
但我实际上需要如下。 (日期时间应该排在第一位。)
{
"dateTime": "2016-01-01",
"name": "MyName",
"id": "123"
}
有什么办法可以保持继承吗?
最佳答案
也许有点晚了,但以防万一有这个注释@JsonPropertyOrder
关于java - toJson() 时是否可以更改属性的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38866839/