Java Jackson循环依赖

Java Jackson循环依赖,java,json,spring,jackson,circular-dependency,Java,Json,Spring,Jackson,Circular Dependency,我有一个循环依赖,我现在正努力解决这个问题 以这两个类为例-为了演示目的删除了锅炉板代码 第一类 @Entity @Table(name = "T_CREDENTIAL") @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") public class Credent

我有一个循环依赖,我现在正努力解决这个问题 以这两个类为例-为了演示目的删除了锅炉板代码

第一类

@Entity
@Table(name = "T_CREDENTIAL")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Credential implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
    //Also fetch this in an eager fashion instead of lazily loading it
    @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
    private Set<UserTask> userTasks = new HashSet<>();

    ....

    .....
    @Entity
@Table(name = "T_CREDENTIAL")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Credential implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -8696943454186195541L;



    //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
    //Also fetch this in an eager fashion instead of lazily loading it
    @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
    @JsonManagedReference("credential")
    private Set<UserTask> userTasks = new HashSet<>();
    ....
....
不幸的是,我有UserTask需要加载凭据的用例和Credential需要加载UserTask的用例

注释@JsonIdentityInfo似乎正在完成它的工作 如果我加载所有UserTasks,它将加载第一个userTask及其凭据,但该凭据对象也将加载分配给该凭据的任何UserTasks。然后会遇到新的@Id或userTask,它现在使用凭证加载它,而不是作为2个users任务加载

有什么办法可以绕过这个问题吗

干杯 达米恩

--问题更新 我现在已经更新了代码,以使用其他用户提到的新注释

第一类

@Entity
@Table(name = "T_CREDENTIAL")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Credential implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
    //Also fetch this in an eager fashion instead of lazily loading it
    @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
    private Set<UserTask> userTasks = new HashSet<>();

    ....

    .....
    @Entity
@Table(name = "T_CREDENTIAL")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Credential implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -8696943454186195541L;



    //Removed @JsonIgnore as want to disable certain fields if no credentials are available    
    //Also fetch this in an eager fashion instead of lazily loading it
    @OneToMany(mappedBy = "credential",fetch=FetchType.EAGER)
    @JsonManagedReference("credential")
    private Set<UserTask> userTasks = new HashSet<>();
    ....
....
这些类现在可以工作,并且没有循环依赖关系
但是,当我现在执行查询以获取usertasks或单个任务时,即使没有指定@jsonIgnore注释,也不会返回凭证对象。你知道是什么原因吗?

如果你正在使用Jackson HttpMessageConverter,你应该检查他们的文档-

您应该添加如下所示的注释:

public class NodeList
{
    @JsonManagedReference
    public List<NodeForList> nodes;
}

public class NodeForList
{
    public String name;

    @JsonBackReference public NodeList parent;

    public NodeForList() { this(null); }
    public NodeForList(String n) { name = n; }
}
公共类节点列表
{
@JsonManagedReference
公共列表节点;
}
公共类节点列表
{
公共字符串名称;
@JsonBackReference公共节点列表父节点;
public NodeForList(){this(null);}
公共节点列表(字符串n){name=n;}
}

我最后选择了下面的注释

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")

感谢你们的帮助,伙计们

Jackson为管理循环引用提供了一些注释。请参阅并感谢kuujo-我添加了这些,它似乎可以工作,但现在我的凭证对象不会从json调用的UserTasks对象返回。我也用这些细节更新了问题。知道上面是什么吗?我没有@JsonIgnore指定的hanks Nikolay-我添加了这些,它似乎可以工作,但现在我的凭证对象没有从json调用的UserTasks对象返回。我也用这些细节更新了问题。知道上面是什么吗?我没有@JsonIgnore指定的hello,在这种情况下,如果您希望在UserTask中序列化
凭证
对象,则应返回
@JsonIdentityInfo
。删除
@JsonBack
@JsonManagedReference
注释,并将
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property=“id”)
添加到类中