[英]Java to JSON serialization with Jackson PTH and Spring Data MongoDB DBRef generates extra target property
當從Java序列化為JSON時,當使用具有延遲加載和Jackson的多態類型處理的Spring Data MongoDB @DBRef
注釋時,Jackson為引用的實體生成額外的target
屬性。 為什么會發生這種情況,是否可以省略額外的target
屬性?
代碼示例
@Document(collection = "cdBox")
public class CDBox {
@Id
public String id;
@DBRef(lazy = true)
public List<Product> products;
}
@Document(collection = "album")
public class Album extends Product {
@DBRef(lazy = true)
public List<Song> songs;
}
@Document(collection = "single")
public class Single extends Product {
@DBRef(lazy = true)
public List<Song> songs;
}
@Document(collection = "song")
public class Song {
@Id
public String id;
public String title;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
property = "productType",
include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
@JsonSubTypes(value = {
@JsonSubTypes.Type(value = Single.class),
@JsonSubTypes.Type(value = Album.class)
})
public abstract class Product {
@Id
public String id;
}
生成的JSON
{
"id": "someId1",
"products": [
{
"id": "someId2",
"songs": [
{
"id": "someId3",
"title": "Some title",
"target": {
"id": "someId3",
"title": "Some title"
}
}
]
}
]
}
目標字段由Spring Data添加,因為它是一個惰性集合。 所以它就像Hibernate for JPA中的datahandler等。
選項1:要忽略它們,您只需在類級別添加@JsonIgnoreProperties(value = {“target”})
@Document(collection = "song")
@JsonIgnoreProperties(value = { "target" })
public class Song {
...
}
選項2:使集合不懶惰
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.