JavaBean中屬性 順序的問題
阿新 • • 發佈:2018-11-23
在程式碼實踐中發下這樣一個有趣的問題,求大佬解釋?
public class ModelData {
@Getter @Setter
private String name;
@Getter @Setter
private String desc;
@Getter @Setter
private String formula;// 規則:all[所有都需要滿足] , least[至少一個滿足], only[有且僅有一個]
@Getter @Setter
private List<ModelData> children;
@Getter @Setter
@com.fasterxml.jackson.annotation.JsonIgnore
private Data data;
public String convert() throws JsonProcessingException {
data = new Data();
data.setModel(this);
return new ObjectMapper().writeValueAsString(data);
}
private class Data implements Cloneable {
private String name;
private String value;
private String formula;//將其放到children前面
private List<Data> children;
@com.fasterxml.jackson.annotation.JsonIgnore
private ModelData model;
public String getName() {
this.name = model.getName();
return name;
}
public String getValue() {
this.value = model.getDesc ();
return value;
}
public String getFormula() {
this.formula = model.getFormula();
return formula;
}
public List<Data> getChildren() {
List<ModelData> list = model.getChildren();
List<Data> temp = new ArrayList<>();
if (list == null || list.isEmpty()) {
return null;
}
for (ModelData model : list) {
this.name = model.name;
this.value = model.desc;
this.formula = model.formula;
this.model = model;
model.data = this;
temp.add(this.clone());
}
return temp;
}
@Override
protected Data clone() {
Data data = null;
try {
data = (Data) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return data;
}
@Override
public String toString() {
return "{name:" + getName() + ",value:" + getValue() + ",formula:" + getFormula() + ",children:["
+ getChildren() + "]" + "}";
}
public void setModel(ModelData model) {
this.model = model;
}
}
}
測試程式碼:
public class BeanPropertyOrderTester {
/**
* 這裡測試用簡單模擬,名稱總是用小寫字母,描述用大寫字母 屬性 formula [all least only]只選擇這三個
* 測試也注意對規則測試,其他全當輔助<br>
* 模型的大致結構如下:<br>
*
* @param args
*/
public static void main(String[] args) {
ModelData j = new ModelData();
j.setName("j");
j.setDesc("J");
j.setFormula("");
ModelData k = new ModelData();
k.setName("k");
k.setDesc("K");
k.setFormula("");
ModelData e = new ModelData();
e.setName("e");
e.setDesc("E");
e.setFormula("all");
List<ModelData> ec = new ArrayList<>();
ec.add(j);
ec.add(k);
e.setChildren(ec);
ModelData l = new ModelData();
l.setName("l");
l.setDesc("L");
l.setFormula("");
ModelData m = new ModelData();
m.setName("m");
m.setDesc("M");
m.setFormula("");
ModelData f = new ModelData();
f.setName("f");
f.setDesc("F");
f.setFormula("least");
List<ModelData> fc = new ArrayList<>();
fc.add(l);
fc.add(m);
f.setChildren(fc);
ModelData n = new ModelData();
n.setName("n");
n.setDesc("N");
n.setFormula("");
ModelData g = new ModelData();
g.setName("g");
g.setDesc("G");
g.setFormula("only");
List<ModelData> gc = new ArrayList<>();
gc.add(n);
g.setChildren(gc);
ModelData b = new ModelData();
b.setName("b");
b.setDesc("B");
b.setFormula("least");
List<ModelData> bc = new ArrayList<>();
bc.add(e);
bc.add(f);
bc.add(g);
b.setChildren(bc);
ModelData h = new ModelData();
h.setName("h");
h.setDesc("H");
h.setFormula("");
ModelData i = new ModelData();
i.setName("i");
i.setDesc("I");
i.setFormula("");
ModelData c = new ModelData();
c.setName("c");
c.setDesc("C");
c.setFormula("only");
List<ModelData> cc = new ArrayList<>();
cc.add(h);
cc.add(i);
c.setChildren(cc);
ModelData d = new ModelData();
d.setName("d");
d.setDesc("D");
d.setFormula("");
ModelData a = new ModelData();
a.setName("a");
a.setDesc("A");
a.setFormula("all");
List<ModelData> ac = new ArrayList<>();
ac.add(b);
ac.add(c);
ac.add(d);
a.setChildren(ac);
String json = null;
try {
json = a.convert();
} catch (JsonProcessingException e1) {
e1.printStackTrace();
}
System.out.println(json);
}
}
說明:
上面程式碼中@[email protected]提供屬性的get和set方法,轉Json方法是使用jackson工具包中的方法;
功能程式碼說明:
後臺有一個模型資料(ModelData),要展示給前臺,但是某些欄位項需要經過轉換後才能給前臺,程式碼中資料我已經將模型簡化
模型物件是:ModelData
,給前臺的物件是Data
ModelData
中包涵的屬性有 name
,desc
,formula
,和children
;而前臺Data
中的屬性是 name
,value
,formula
和children
,顯然定義的屬性名稱有些是不一樣
(不要糾結為啥前後臺定義的屬性不統一)
這裡要討論的是一個bean中屬性順序的問題.
這裡還有個注意的問題就是,在ModelData
中包涵一個Data
屬性,在Data
中包涵一個ModelData
屬性
這樣做的目的: 為了在將ModelData
轉換稱Data
時候,對children
的簡便處理,否則的話,對children
的處理感覺會巨麻煩(自己能力有限,歡迎大佬指正),就是children
裡面各個物件下還有children
,無限層級的
這裡需要注意了Data
裡面的屬性順序:在Data
裡面 屬性children
在所有的欄位中是最後的,執行一遍測試:可以看出結果完美呈現,如下:
{
"name": "a",
"value": "A",
"formula": "all",
"children": [{
"name": "b",
"value": "B",
"formula": "least",
"children": [{
"name": "e",
"value": "E",
"formula": "all",
"children": [{
"name": "j",
"value": "J",
"formula": "",
"children": null
}, {
"name": "k",
"value": "K",
"formula": "",
"children": null
}
]
}, {
"name": "f",
"value": "F",
"formula": "least",
"children": [{
"name": "l",
"value": "L",
"formula": "",
"children": null
}, {
"name": "m",
"value": "M",
"formula": "",
"children": null
}
]
}, {
"name": "g",
"value": "G",
"formula": "only",
"children": [{
"name": "n",
"value": "N",
"formula": "",
"children": null
}
]
}
]
}, {
"name": "c",
"value": "C",
"formula": "only",
"children": [{
"name": "h",
"value": "H",
"formula": "",
"children": null
}, {
"name": "i",
"value": "I",
"formula": "",
"children": null
}
]
}, {
"name": "d",
"value": "D",
"formula": "",
"children": null
}
]
}
上面結果正是我說需要的要展示給前臺的資料;
然而,這裡調一下屬性的位置,在Data
中,將formula
欄位位置放到屬性children
的後面,
...
private class Data implements Cloneable {
private String name;
private String value;
private List<Data> children;
private String formula;//將其放到children後面
@com.fasterxml.jackson.annotation.JsonIgnore
private ModelData model;
...// 後面都一樣
再執行一遍測試:就會發現farmula的資料大部分沒有了,如下:
{
"name": "a",
"value": "A",
"children": [{
"name": "b",
"value": "B",
"children": [{
"name": "e",
"value": "E",
"children": [{
"name": "j",
"value": "J",
"children": null,
"formula": ""
}, {
"name": "k",
"value": "K",
"children": null,
"formula": ""
}
],
"formula": ""
}, {
"name": "f",
"value": "F",
"children": [{
"name": "l",
"value": "L",
"children": null,
"formula": ""
}, {
"name": "m",
"value": "M",
"children": null,
"formula": ""
}
],
"formula": ""
}, {
"name": "g",
"value": "G",
"children": [{
"name": "n",
"value": "N",
"children": null,
"formula": ""
}
],
"formula": ""
}
],
"formula": "only"
}, {
"name": "c",
"value": "C",
"children": [{
"name": "h",
"value": "H",
"children": null,
"formula": ""
}, {
"name": "i",
"value": "I",
"children": null,
"formula": ""
}
],
"formula": ""
}, {
"name": "d",
"value": "D",
"children": null,
"formula": ""
}
],
"formula": ""
}
僅僅調了一下屬性的位置啊,這是為什麼呢?求大佬解答,歡迎評論,或加微信交流:junehappylove