【问题标题】:Avoiding Infinite Loop Spring Boot避免无限循环 Spring Boot
【发布时间】:2022-01-15 17:22:03
【问题描述】:

在 Spring Boot Rest API 中,我能够使用 @JsonIgnore 避免无限循环。在邮递员结果中,相关列表(多方)显示为空。当我将在 Angular 中使用此端点时,即使在我的邮递员中被 @JsonIgnore 跳过,我是否能够显示该相关列表?

考虑 MatierePlannificationConcours 之间的关系,它有一些其他的孩子,如何避免无限循环并返回 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
}```

【问题讨论】:

  • 请分享相关代码,并尝试通过用例更清楚您的问题

标签: angular spring spring-boot rest spring-data


【解决方案1】:

您必须首先了解您的前端(Angular)和后端(Spring Boot)是两个独立的应用程序。

我能够使用@JsonIgnore 避免无限循环。在邮递员 结果,相关列表(多方)显示为空。

因此,您已通过修改返回对象转换为 JSON 对象的方式来修改后端以不进入不定式循环。如果您的后端在转换为 JSON 对象时会陷入不定式循环,现在将在此时写入空值以避免不定式循环。

当我将在 Angular 中使用此端点时,我是否能够显示 该相关列表即使在我的邮递员中被@JsonIgnore 跳过

您的 Angular 应用程序将收到您的邮递员收到的内容,也就是您后端的答案。所以答案是否定的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 2021-09-21
    • 2021-03-07
    相关资源
    最近更新 更多