Home > Net >  Avoiding Infinite Loop Spring Boot
Avoiding Infinite Loop Spring Boot

Time:12-12

In a Spring Boot Rest API, I was able to avoid infinite loop with @JsonIgnore. In the postman result, the related list (many side) show null. 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?

Consisidering a relationship between Matiere and PlannificationConcours which has some other child, how to avoid infinite loop and and return null value.

the one side

@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() {
    }

The many side

@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;
    }
}

Postman Response

    "id": 2,
    "description": "Prgrammation C#",
    "code": "C Sharp",
    "nombreDeCreditStandard": 2,
    "cours": [],
    "contenu": "C#",
    "plannificationConcours": null
}```

CodePudding user response:

You must first understand that your Frontend (Angular) and Backend(Spring Boot) are 2 independent applications.

I was able to avoid infinite loop with @JsonIgnore. In the postman result, the related list (many side) show null.

So you have modified your backend to not get into infinitive loop by modifying how the returned object is converted into a JSON object. Where your backend would be stuck into a infinitive loop on the conversion to JSON object, now a null value would be written at this point to avoid infinitive loop.

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

Your angular application will receive exactly what your postman receives which is the answer from your backend. So the answer is no.

  • Related