直接自引用导致循环超类问题JSON

Tin*_*son 9 java inheritance json jax-rs

我尝试了一些我在搜索时发现的东西,但没有任何帮助,或者我没有正确实现它.

我得到的错误

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

这两个都扩展了基本实体,并且在基础(超类)中也有它appInstance.

基本实体看起来与此类似

@MappedSuperclass
public abstract class BaseEntity implements Comparable, Serializable {

@ManyToOne
protected ApplicationInstance appInstance;

//getter & setter

}
Run Code Online (Sandbox Code Playgroud)

应用程序实体如下所示

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.)
}
Run Code Online (Sandbox Code Playgroud)

特殊实体

@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(); 
  }
}
Run Code Online (Sandbox Code Playgroud)

Special中的所有实体都继承自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;
}
Run Code Online (Sandbox Code Playgroud)

在特殊实体上我尝试了以下内容

  • 添加了jsonIgnoreProperties
  • 添加了appInstance的覆盖,以便使用@JsonIgnore进行批注
  • @JsonIdentityInfo

上面的链接

这些解决方案都不起作用.难道我做错了什么?

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

Pab*_*ano 3

通常,排除响应中的属性就像@JsonIgnore向其 getter 添加注释一样简单,但如果您不想将此注释添加到父类,则可以覆盖 getter,然后在其上添加注释:

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

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

另一种选择,更“手动”,只是为响应创建一个 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
    }

}
Run Code Online (Sandbox Code Playgroud)