【发布时间】:2018-01-24 15:37:51
【问题描述】:
我在搜索时尝试了几件事,但没有任何帮助,或者我没有正确实施。
我得到的错误
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();
}
}
Special 中的所有实体都继承自包含 AppInstance 的 BaseEntity
那我有办法得到特殊的
@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
以上链接
- https://stackoverflow.com/a/29632358/4712391
- Jackson serialization: how to ignore superclass properties
- jackson self reference leading to cycle
这些解决方案都不起作用。我做错了吗?
注意:是否也可以编辑特殊的,因为其他实体在不同的包中并且不想编辑它们。
【问题讨论】:
-
将
protected ApplicationInstance appInstance设为私有并为其添加一些getter和setter(受保护的或公共的),然后用@JsonIgnore覆盖ApplicationInstance中的getter注释。 -
@aristotll 我添加了更多细节,但认为不会改变。都引用应用实例
-
澄清一下:您只能从该实例中排除 appInstance,这就是为什么您不能在映射类上添加
@JsonIgnore? -
@Mark 如有必要,我可以从任何地方排除 appInstance。宁愿避免对 BaseEntity 以及 Appinstance 本身进行更改。 Special 确实包含其他带有 appinstance 的实体,我应该全部排除它吗?
-
可能是的,但很容易判断您是否可以包含整个实体定义。可以加吗?我也可以自己测试一下。
标签: java json inheritance jax-rs