0
我有這樣一個主要目的:連載對象實例基本屬性傑克遜
public class MainObject{
@Column(unique = false, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0)
@Basic
private String name;
@JsonIgnoreProperties({"address" ... })
@ManyToOne(optional = true, targetEntity = Company.class)
private Child child;
}
我想只有一個孩子爲對象的「基本」型的屬性序列化此。正如你可以看到我的最佳解決方案是忽略子對象的所有其他屬性。
我的目標是得到這樣的:
{
"id" = 12,
"name" = "some name",
"company" = 42
}
但到目前爲止我最好的解決辦法,巫婆你可以看到上我得到以下:
{
"id" = 12,
"name" = "some name",
"company" = {
"id" = 42
}
}
我知道有註釋女巫我可以在子對象中使用,但是這裏來了棘手的部分,在我的程序中的其他部分,我想正常序列化孩子。
public class Child{
@Column(unique = false, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0)
@Basic
private String name;
@Column(unique = true, updatable = true, insertable = true, nullable = true, length = 255, scale = 0, precision = 0)
@Basic
private String address;
...
}
有沒有一種簡單的方法來獲得我想要的內容?
不是那麼簡單,但可行的http:// www.baeldung.com/jackson-custom-serialization – gigadot