我嘗試了幾次搜索時發現的事情,但沒有任何幫助,或者我沒有正確實現它。導致循環超類問題的直接自引用JSON
錯誤我得到
Direct self-reference leading to cycle (through reference chain: io.test.entity.bone.Special["appInstance"]->io.test.entity.platform.ApplicationInstance["appInstance"])
這兩個擴展基礎機構,並在基地(超類),它有一個appInstance
爲好。
基礎機構類似於這樣
@MappedSuperclass
public abstract class BaseEntity implements Comparable, Serializable {
@ManyToOne
protected ApplicationInstance appInstance;
//getter & setter
}
應用實體看起來像這樣
public class ApplicationInstance extends BaseEntity implements Serializable {
private List<User> users;
// some other properties (would all have the same base and application instance . User entity will look similar to the Special.)
}
特殊實體
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType")
@JsonIgnoreProperties({"createdBy", "appInstance", "lastUpdatedBy"})
public class Special extends BaseEntity implements Serializable {
@NotNull
@Column(nullable = false)
private String name;
@Column(length = Short.MAX_VALUE)
private String description;
@NotNull
@Column(nullable = false)
private Double price;
@OneToOne
private Attachment image;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = SpecialTag.class)
@CollectionTable(name = "special_tags")
@Column(name = "specialtag")
private List<SpecialTag> specialTags;
@Temporal(TemporalType.TIME)
private Date specialStartTime;
@Temporal(TemporalType.TIME)
private Date specialEndTime;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = WeekDay.class)
@CollectionTable(name = "available_week_days")
@Column(name = "weekday")
private List<WeekDay> availableWeekDays;
@OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
private List<SpecialStatus> statuses;
@OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
private List<SpecialReview> specialReviews;
@Transient
private Integer viewed;
private Boolean launched;
@OneToMany(mappedBy = "special")
private List<CampaignSpecial> specialCampaigns;
@Override
@JsonIgnore
public ApplicationInstance getAppInstance() {
return super.getAppInstance();
}
}
特殊的所有實體從BaseEntity繼承包含AppInstance
然後我有一個方法來獲得特殊
@GET
@Path("{ref}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.TEXT_PLAIN)
public Special findByGuestRef(@PathParam("ref") String pRefeference) {
// find the special and return it
return special;
}
在我嘗試以下
- 新增jsonIgnoreProperties
- 增加了一個覆蓋了appInstance與@JsonIgnore
- 註釋的特殊實體@JsonIdentityInfo
abov的鏈接Ë
- https://stackoverflow.com/a/29632358/4712391
- Jackson serialization: how to ignore superclass properties
- jackson self reference leading to cycle
沒有這些解決方案的工作。難道我做錯了什麼?
注意:是否也可以編輯特殊的,因爲其他實體位於不同的包中,並且不想編輯它們。
使'protected ApplicationInstance appInstance'專用併爲其添加一些getter(s)和setter(s)(protected或public),然後使用'@ JsonIgnore'註釋覆蓋'ApplicationInstance'中的getter(s)。 – ConductedClever
@aristotll我已經添加了一些細節,但不認爲它會改變。所有引用應用程序實例 –
只是爲了澄清:你只會從這個實例中排除appInstance,這就是爲什麼你不能在映射類中添加'@ JsonIgnore'? – Mark