gpt4 book ai didi

java - 直接自引用导致循环父类(super class)问题 JSON

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:24:04 25 4
gpt4 key购买 nike

我尝试了一些在搜索时发现的方法,但没有任何帮助,或者我没有正确实现。

我得到的错误

Direct self-reference leading to cycle (through reference chain: io.test.entity.bone.Special["appInstance"]->io.test.entity.platform.ApplicationInstance["appInstance"])

它们都扩展了基础实体,并且在基础(父类(super class))中它也有一个 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

上面的链接

这些解决方案都不起作用。我做错了什么吗?

注意:是否也可以只编辑特殊的,因为其他实体在不同的包中并且不想编辑它们。

最佳答案

通常在响应中排除属性就像在它们的 getter 中添加一个 @JsonIgnore 注解一样简单,但是如果你不想在父类中添加这个注解,你可以覆盖 getter然后在上面添加注解:

public class Special extends BaseEntity implements Serializable {
...
@JsonIgnore
public ApplicationInstance getAppInstance() {
return this.appInstance;
}
...
}

注意:由于有多个框架,请确保您使用的是正确的 @JsonIgnore 注释,否则它将被忽略,请参阅 this answer例如。

另一个选项,更“手动”,只是为响应创建一个 bean,它将是特殊实例的一个子集:

@GET
@Path("{ref}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.TEXT_PLAIN)
public SpecialDTO findByGuestRef(@PathParam("ref") String pRefeference) {
// find the special and return it
return new SpecialDTO(special);
}


public class SpecialDTO {

//declare here only the attributes that you want in your response

public SpecialDTO(Special sp) {
this.attr=sp.attr; // populate the needed attributes
}

}

关于java - 直接自引用导致循环父类(super class)问题 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45714483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com