避免无限循环 Spring 启动
Avoiding Infinite Loop Spring Boot
在Spring Boot Rest API 中,我能够使用@JsonIgnore
避免无限循环。在邮递员结果中,相关列表(多边)显示为空。当我将在 Angular 中使用此端点时,即使它在我的邮递员中被 @JsonIgnore
跳过,我是否能够显示该相关列表?
考虑 Matiere 和 PlannificationConcours 之间的关系,其中有一些其他 child,如何避免无限循环和 return null
值。
一侧
@Data
@AllArgsConstructor
@Table(name="Matiere")
public class Matiere extends Audit<String> implements Serializable {
@Column(name="ID", nullable=false, length=10)
@Id
@GeneratedValue(generator="PNU_MATIERE_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name="PNU_MATIERE_ID_GENERATOR", strategy="native")
private int id;
// @ManyToOne(targetEntity=fdsa.edu.PNUFDSA.Model.Matiere.class, fetch=FetchType.LAZY)
// @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})
// @JoinColumns(value={ @JoinColumn(name="MatiereID", referencedColumnName="ID", nullable=false) }, foreignKey=@ForeignKey(name="Pre-requis"))
// private fdsa.edu.PNUFDSA.Model.Matiere matiere;
@Column(name="Description", nullable=true, length=255)
private String description;
@Column(name="Code", nullable=true, length=255)
private String code;
@Column(name="Contenu", nullable=true, length=255)
private String Contenu;
@Column(name="NombreDeCreditStandard", nullable=false, length=10)
private int nombreDeCreditStandard;
@OneToMany(mappedBy="matiere", targetEntity= Cours.class)
private List<Cours> cours ;
@JsonManagedReference
@OneToMany(mappedBy="matiere", targetEntity= PlannificationConcours.class)
private List<PlannificationConcours> plannificationConcourses;
public Matiere() {
}
多面
@Entity
@AllArgsConstructor
@Table(name="PlannificationConcours")
public class PlannificationConcours extends Audit<String> implements Serializable {
public PlannificationConcours() {
}
@Column(name="ID", nullable=false, length=10)
@Id
@GeneratedValue(generator="PNU_PLANNIFICATIONCONCOURS_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name="PNU_PLANNIFICATIONCONCOURS_ID_GENERATOR", strategy="native")
private int id;
@ManyToOne (targetEntity= Concours.class, fetch=FetchType.LAZY)
@JoinColumns(value={ @JoinColumn(name="concoursId", referencedColumnName="ID", nullable=true) }, foreignKey=@ForeignKey(name="ConcoursPlanificationConcours"))
//@JsonBackReference
private Concours concours;
@JsonBackReference
@ManyToOne(targetEntity= Matiere.class, fetch=FetchType.LAZY)
@JoinColumns(value={ @JoinColumn(name="MatiereId", referencedColumnName="ID", nullable=true) }, foreignKey=@ForeignKey(name="MatierePlanificationConcours"))
private Matiere matiere;
@Column(name="`Date`", nullable=true)
@Temporal(TemporalType.DATE)
private java.util.Date Date;
@Column(name="Quotation", nullable=false, length=10)
private double quotation;
@Column(name="NoteDePassage", nullable=false, length=10)
private double noteDePassage;
@OneToMany(mappedBy="plannificationConcours", cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity= HistoriqueExamenConcours.class)
private List<HistoriqueExamenConcours> historiqueExamenConcours;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Concours getConcours() {
return concours;
}
public void setConcours(Concours concours) {
this.concours = concours;
}
public Matiere getMatiere() {
return matiere;
}
public void setMatiere(Matiere matiere) {
this.matiere = matiere;
}
public java.util.Date getDate() {
return Date;
}
public void setDate(java.util.Date date) {
Date = date;
}
public double getQuotation() {
return quotation;
}
public void setQuotation(double quotation) {
this.quotation = quotation;
}
public double getNoteDePassage() {
return noteDePassage;
}
public void setNoteDePassage(double noteDePassage) {
this.noteDePassage = noteDePassage;
}
public List<HistoriqueExamenConcours> getHistoriqueExamenConcours() {
return historiqueExamenConcours;
}
public void setHistoriqueExamenConcours(List<HistoriqueExamenConcours> historiqueExamenConcours) {
this.historiqueExamenConcours = historiqueExamenConcours;
}
}
邮递员回复
"id": 2,
"description": "Prgrammation C#",
"code": "C Sharp",
"nombreDeCreditStandard": 2,
"cours": [],
"contenu": "C#",
"plannificationConcours": null
}```
你必须先明白你的Frontend(Angular)和Backend(Spring Boot)是2个独立的应用程序。
I was able to avoid infinite loop with @JsonIgnore. In the postman
result, the related list (many side) show null.
因此,您已通过修改返回对象转换为 JSON 对象的方式来修改后端,使其不进入无限循环。如果您的后端在转换为 JSON 对象时会陷入无限循环,现在将在此时写入一个空值以避免无限循环。
When i will use this endpoint in Angular, will i be able to display
that related list even if it is skipped by @JsonIgnore in my postman
您的 angular 应用程序将收到您的邮递员收到的确切信息,即来自您后端的答复。所以答案是否定的。
在Spring Boot Rest API 中,我能够使用@JsonIgnore
避免无限循环。在邮递员结果中,相关列表(多边)显示为空。当我将在 Angular 中使用此端点时,即使它在我的邮递员中被 @JsonIgnore
跳过,我是否能够显示该相关列表?
考虑 Matiere 和 PlannificationConcours 之间的关系,其中有一些其他 child,如何避免无限循环和 return null
值。
一侧
@Data
@AllArgsConstructor
@Table(name="Matiere")
public class Matiere extends Audit<String> implements Serializable {
@Column(name="ID", nullable=false, length=10)
@Id
@GeneratedValue(generator="PNU_MATIERE_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name="PNU_MATIERE_ID_GENERATOR", strategy="native")
private int id;
// @ManyToOne(targetEntity=fdsa.edu.PNUFDSA.Model.Matiere.class, fetch=FetchType.LAZY)
// @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})
// @JoinColumns(value={ @JoinColumn(name="MatiereID", referencedColumnName="ID", nullable=false) }, foreignKey=@ForeignKey(name="Pre-requis"))
// private fdsa.edu.PNUFDSA.Model.Matiere matiere;
@Column(name="Description", nullable=true, length=255)
private String description;
@Column(name="Code", nullable=true, length=255)
private String code;
@Column(name="Contenu", nullable=true, length=255)
private String Contenu;
@Column(name="NombreDeCreditStandard", nullable=false, length=10)
private int nombreDeCreditStandard;
@OneToMany(mappedBy="matiere", targetEntity= Cours.class)
private List<Cours> cours ;
@JsonManagedReference
@OneToMany(mappedBy="matiere", targetEntity= PlannificationConcours.class)
private List<PlannificationConcours> plannificationConcourses;
public Matiere() {
}
多面
@Entity
@AllArgsConstructor
@Table(name="PlannificationConcours")
public class PlannificationConcours extends Audit<String> implements Serializable {
public PlannificationConcours() {
}
@Column(name="ID", nullable=false, length=10)
@Id
@GeneratedValue(generator="PNU_PLANNIFICATIONCONCOURS_ID_GENERATOR")
@org.hibernate.annotations.GenericGenerator(name="PNU_PLANNIFICATIONCONCOURS_ID_GENERATOR", strategy="native")
private int id;
@ManyToOne (targetEntity= Concours.class, fetch=FetchType.LAZY)
@JoinColumns(value={ @JoinColumn(name="concoursId", referencedColumnName="ID", nullable=true) }, foreignKey=@ForeignKey(name="ConcoursPlanificationConcours"))
//@JsonBackReference
private Concours concours;
@JsonBackReference
@ManyToOne(targetEntity= Matiere.class, fetch=FetchType.LAZY)
@JoinColumns(value={ @JoinColumn(name="MatiereId", referencedColumnName="ID", nullable=true) }, foreignKey=@ForeignKey(name="MatierePlanificationConcours"))
private Matiere matiere;
@Column(name="`Date`", nullable=true)
@Temporal(TemporalType.DATE)
private java.util.Date Date;
@Column(name="Quotation", nullable=false, length=10)
private double quotation;
@Column(name="NoteDePassage", nullable=false, length=10)
private double noteDePassage;
@OneToMany(mappedBy="plannificationConcours", cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity= HistoriqueExamenConcours.class)
private List<HistoriqueExamenConcours> historiqueExamenConcours;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Concours getConcours() {
return concours;
}
public void setConcours(Concours concours) {
this.concours = concours;
}
public Matiere getMatiere() {
return matiere;
}
public void setMatiere(Matiere matiere) {
this.matiere = matiere;
}
public java.util.Date getDate() {
return Date;
}
public void setDate(java.util.Date date) {
Date = date;
}
public double getQuotation() {
return quotation;
}
public void setQuotation(double quotation) {
this.quotation = quotation;
}
public double getNoteDePassage() {
return noteDePassage;
}
public void setNoteDePassage(double noteDePassage) {
this.noteDePassage = noteDePassage;
}
public List<HistoriqueExamenConcours> getHistoriqueExamenConcours() {
return historiqueExamenConcours;
}
public void setHistoriqueExamenConcours(List<HistoriqueExamenConcours> historiqueExamenConcours) {
this.historiqueExamenConcours = historiqueExamenConcours;
}
}
邮递员回复
"id": 2,
"description": "Prgrammation C#",
"code": "C Sharp",
"nombreDeCreditStandard": 2,
"cours": [],
"contenu": "C#",
"plannificationConcours": null
}```
你必须先明白你的Frontend(Angular)和Backend(Spring Boot)是2个独立的应用程序。
I was able to avoid infinite loop with @JsonIgnore. In the postman result, the related list (many side) show null.
因此,您已通过修改返回对象转换为 JSON 对象的方式来修改后端,使其不进入无限循环。如果您的后端在转换为 JSON 对象时会陷入无限循环,现在将在此时写入一个空值以避免无限循环。
When i will use this endpoint in Angular, will i be able to display that related list even if it is skipped by @JsonIgnore in my postman
您的 angular 应用程序将收到您的邮递员收到的确切信息,即来自您后端的答复。所以答案是否定的。