Java com.fasterxml.jackson.databind.ser.BeanSerializer.serialize Spring JPA
在春天,我的班级之间有一对多的关系。当我尝试登录时,我得到了错误,就像无限递归,这是整个错误消息Java com.fasterxml.jackson.databind.ser.BeanSerializer.serialize Spring JPA,java,json,spring,spring-mvc,jackson,Java,Json,Spring,Spring Mvc,Jackson,在春天,我的班级之间有一对多的关系。当我尝试登录时,我得到了错误,就像无限递归,这是整个错误消息 at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsFie
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.10.jar:2.8.10]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689) ~[jackson-databind-2.8.10.jar:2.8.10]
我有5个相互连接的类。
这是:
@Entity
public class AppUser implements UserDetails {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@Column(unique = true)
private String username;
private String password;
@ElementCollection(fetch = FetchType.EAGER)
private List<String> roles = new ArrayList<>();
@ManyToOne
@JoinColumn(name = "apartmen_id")
@JsonIgnoreProperties(value = {"apartmen_tenats"}, allowSetters=true)
private Apartmen apartmen; // apartmen in which he lives
@ManyToOne
@JoinColumn(name = "institution_id")
@JsonIgnoreProperties(value = {"workers"}, allowSetters=true)
private Institution institution; // institution in which he works
@OneToMany(mappedBy = "worker")
@JsonIgnoreProperties(value = {"worker"}, allowSetters = true)
private Set<Failure> failures; // Kvarovi na kojima je radio
}
第三类:
@Entity
public class Building implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String location;
private String owner; // Vlasnik zgrade
private int numberOfApartments;
private int numberOfAparartmentsWithTenats; // Broj stanova koji su naseljeni
private boolean hasPresident; // Oznacava da li zgrada ima predsednika skupstine stanara
@OneToMany(mappedBy = "apartmenBuilding")
@JsonIgnoreProperties(value = {"apartmenBuilding"}, allowSetters = true)
private Set<Apartmen> apartments; // stanovi u zgradi
@ManyToMany(mappedBy = "buildings")
@JsonIgnoreProperties(value = {"buildings"}, allowSetters = true)
private Set<Institution> institutions;
@OneToMany(mappedBy = "building", fetch = FetchType.EAGER)
@JsonIgnoreProperties(value = {"building"}, allowSetters = true)
private List<Failure> failures; // Kvarovi koji su nastali u zgradi
}
@实体
公共类构建实现了可序列化{
/**
*
*/
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私有字符串名称;
私有字符串位置;
私有字符串所有者;//Vlasnik zgrade
私人公寓;
私人公寓的国际号码;//Broj stanova koji su naseljeni
二等兵总统;//奥扎卡瓦·达利·兹格拉达·伊玛·普雷塞德尼卡·斯库普斯廷·斯塔纳拉
@OneToMany(mappedBy=“apartmenBuilding”)
@JsonIgnoreProperties(值={“apartmenBuilding”},allowSetters=true)
私人套房;//stanovi u zgradi
@许多(mappedBy=“建筑物”)
@JsonIgnoreProperties(值={“建筑物”},allowSetters=true)
私营机构;
@OneToMany(mappedBy=“building”,fetch=FetchType.EAGER)
@JsonIgnoreProperties(值={“building”},allowSetters=true)
私有列表失败;//Kvarovi koji su nastali u zgradi
}
第四类:
@Entity
public class Institution {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String location;
private String director;
private String email;
private String contactPhone;
private String webSiteUrl;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
@JoinTable(name = "institution_building",
joinColumns = @JoinColumn(name = "institution_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "building_id", referencedColumnName = "id"))
@JsonIgnoreProperties(value = {"institutions"}, allowSetters=true)
private Set<Building> buildings; // Buildings which this institution is maintaining
@OneToMany(mappedBy = "institution")
@JsonIgnoreProperties(value = {"institution"}, allowSetters = true)
private Set<AppUser> workers;
@OneToMany(mappedBy = "institution", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JsonIgnoreProperties(value = {"institution"}, allowSetters = true)
private Set<Failure> failures; // Kvarovi na kojima je radila institucija
}
@实体
公办院校{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私有字符串名称;
私有字符串位置;
私有字符串控制器;
私人字符串电子邮件;
私用线控电话;
私有字符串网址;
@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.MERGE)
@可接合(name=“机构建筑”,
joinColumns=@JoinColumn(name=“institution\u id”,referencedColumnName=“id”),
inverseJoinColumns=@JoinColumn(name=“building\u id”,referencedColumnName=“id”))
@JsonIgnoreProperties(值={“机构”},allowSetters=true)
私人设置的建筑物;//该机构正在维护的建筑物
@OneToMany(mappedBy=“机构”)
@JsonIgnoreProperties(值={“机构”},allowSetters=true)
私营部门工人;
@OneToMany(mappedBy=“institution”,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JsonIgnoreProperties(值={“机构”},allowSetters=true)
私有集故障;//Kvarovi na kojima je radila institucija
}
最后一节课是:
@Entity
public class Apartmen implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String location;
private String owner;
private int numberOfTenats;
private boolean hasApartmentBuilding;
private boolean hasOwner;
@ManyToOne
@JoinColumn(name = "building_id")
@JsonIgnoreProperties(value = {"apartments"}, allowSetters=true)
private Building apartmenBuilding;
@OneToMany(mappedBy = "apartmen")
@JsonIgnoreProperties(value = {"apartmen"}, allowSetters = true)
private Set<AppUser> apartmen_tenats;
}
@实体
公共类Apartmen实现可序列化{
/**
*
*/
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私有字符串名称;
私有字符串位置;
私人字符串所有者;
私人国际电话号码;
私人住宅楼;
私人业主;
@许多酮
@JoinColumn(name=“building\u id”)
@JsonIgnoreProperties(值={“公寓”},allowSetters=true)
私人楼宇公寓大厦;
@OneToMany(mappedBy=“apartmen”)
@JsonIgnoreProperties(值={“apartmen”},allowSetters=true)
私人公寓;
}
我不知道为什么会发生这种情况,但可能是因为与课堂失败的关系。我认为可能是这样,因为该类与代码中的其他所有类都有关系。如果是这样的话,你能告诉我为什么会发生这种情况,以及如何正确处理
提前感谢。@JsonIgnoreProperties是类级别的注释,它希望忽略类中的确切字段名 最好的方法是将所有
@JsonIgnoreProperties
替换为
@JsonIgnore.
i、 e
将此更改为
@ManyToOne
@JoinColumn(name = "apartmen_id")
@JsonIgnore
private Apartmen apartmen;
对所有出现的@JsonIgnoreProperties
进行类似更改。
这应该是可行的。基本上是JSON序列化,而不是JPA API。完全不同的过程
@ManyToOne
@JoinColumn(name = "apartmen_id")
@JsonIgnoreProperties(value = {"apartmen_tenats"}, allowSetters=true)
private Apartmen apartmen;
@ManyToOne
@JoinColumn(name = "apartmen_id")
@JsonIgnore
private Apartmen apartmen;