我需要使用外鍵序列化只有兩列的實體。我在Wildfly工作,所以我正在尋找傑克遜解決方案。JAVA JACKSON:使用兩個字段而不是所有類序列化一個類
假設我有實體的A類
public class A{
private Long id;
private String name;
private String anotherinfo;
private String foo;
...
}
和另一個類B:
public class B{
private Long id;
private String name;
private A parent;
}
我想序列化與他的所有領域,當我搜索了,但是當我需要找回B的istance,我只需要兩個字段(一個ID和一個標籤)
如果使用的註釋:
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
private A parent;
我只會返回id。
我想會是怎樣的結果:
B: {
"id" : 1,
"name" : "test",
"parent" : {
"id" : 1,
"name" : 2
}
}
對字段使用關鍵字transient並且它們不會被序列化。 – LearningPhase
http://stackoverflow.com/questions/8179986/jackson-change-jsonignore-dynamically –
瞬態關鍵字可能對此有所幫助。 –