直接自引用导致循环超类问题JSON [英] Direct self-reference leading to cycle Superclass issue JSON

查看:1165
本文介绍了直接自引用导致循环超类问题JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have tried several things I found while searching but nothing helped or I did not implement it correctly.

错误我得到了

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

这两个都扩展了基础实体和基础(超类)它还有 appInstance

Both these extend the base entity and in the base (super class) it has an appInstance as well.

基本实体看起来与此类似

Base entity looks similar to this

@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中的所有实体都继承自BaseEntity,其中包含AppInstance

All entities in Special inherits from BaseEntity which contains AppInstance

然后我有一个获得特殊的方法

then i have a method to get the special

@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;
}

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

On the special entity I tried the following


  • 添加了jsonIgnoreProperties

  • 添加了appInstance的覆盖以使用@JsonIgnore进行注释

  • @JsonIdentityInfo

上述链接

  • https://stackoverflow.com/a/29632358/4712391
  • Jackson serialization: how to ignore superclass properties
  • jackson self reference leading to cycle

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

none of those solutions works. Am I doing something wrong?

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

Note: Would it also just be possible to edit special, since the other entities are in a different package and would not like to edit them.

推荐答案

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

Usually excluding attributes in a response is as easy as adding a @JsonIgnore annotation to their getters, but if you don't want to add this annotation to a parent class, you could override the getter and then add the annotation on it:

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

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

NOTE: As there are several frameworks, make sure that you are using the correct @JsonIgnore annotation or it will be ignored, see this answer for instance.

另一种选择,更手动,只是为了创建一个bean响应将是特殊实例的子集:

Another option, more "manual", is just creating a bean for the response which would be a subset of the Special instance:

@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
    }

}

这篇关于直接自引用导致循环超类问题JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆