Java Jackson注释无效
我一直在尝试使用Jackson注释来避免循环关联,但它似乎并没有像预期的那样工作,而且我仍然会遇到堆栈溢出Java Jackson注释无效,java,json,jackson,Java,Json,Jackson,我一直在尝试使用Jackson注释来避免循环关联,但它似乎并没有像预期的那样工作,而且我仍然会遇到堆栈溢出 过敏原类别: import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import j
过敏原
类别:
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the allergens database table.
*
*/
@Entity
@Table(name="allergens")
@NamedQuery(name="Allergen.findAll", query="SELECT a FROM Allergen a")
//@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Allergen{
private static final long serialVersionUID = 1L;
@Id
private int id;
private boolean isEnabled;
private String title;
//bi-directional many-to-one association to Recipe
@ManyToOne
@JsonManagedReference
private Recipe recipe;
//+getters/setters
配方
类
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.io.Serializable;
import javax.persistence.*;
import java.util.List;
/**
* The persistent class for the recipes database table.
*
*/
@Entity
@Table(name="recipes")
@NamedQuery(name="Recipe.findAll", query="SELECT r FROM Recipe r")
//@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Recipe{
private static final long serialVersionUID = 1L;
@Id
private int id;
private String complexity;
private int cookingTime;
private String description;
private int estimatedTime;
private String imageUrl;
private String information;
private boolean isPromoted;
private int preparationTime;
private float servings;
private String title;
private String type;
//bi-directional many-to-one association to Allergen
@OneToMany(mappedBy="recipe")
@JsonBackReference
private List<Allergen> allergens;
//+getters/setters
jackson还需要什么吗?您可以尝试添加导致循环引用的属性。这将告诉Jackson不要序列化该属性。我尝试使用
com.fasterxml.Jackson.annotation.JsonIgnore
中的@JsonBackReference
和@JsonManagedReference
中的@JsonIgnore
作为过敏原
类中的Recipe对象,但我仍然得到堆栈溢出例外,因此可能问题不在于Jackson,而在于你们的JPA映射?我不知道,怀疑它,我觉得它很好。当我试图使用rest服务的get方法时,会出现此错误。在JBoss上,我可以看到json中的无限循环,但在glassfish上,我得到了直接的stackoverflow异常。此外,该模型是由eclipseI中的某个JPA工具自动创建的。在获取实体后,eclipseI会尝试调试并添加断点,以查看它是否在序列化之前找到。另外,我建议不要直接返回实体,而是返回一个DTO
,它是包含数据的非托管对象。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.8</version>
</dependency>