2016-03-22 1 views
6

Ich habe eine MySQL-Datenbank und ich möchte einige Daten als JSON abrufen.Abrufen von Daten aus der Datenbank als JSON im Frühjahr boot

Und ich habe eine Einheit Offre, die @OneToMany Beziehung mit der AssociationCandidatOffre Einheit hat.

und ich habe eine api, die diese Methode in meinem Repository Calles:

offreRepository.findAll(); 

Offre Einheit:

@Entity 
public class Offre implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "CODE_OFFRE") 
    private Long codeOffre; 
    private String titre; 

    @OneToMany(mappedBy = "offre") 
    private Collection<AssociationCandidatOffre> associationCandidatOffres; 

    public Collection<AssociationCandidatOffre> getAssociationCandidatOffres() { 
     return associationCandidatOffres; 
    } 

    public void setAssociationCandidatOffres(Collection<AssociationCandidatOffre> associationCandidatOffres) { 
     this.associationCandidatOffres = associationCandidatOffres; 


    } 
    //... getters/setters  
    } 

AssociationCandidatOffre Einheit:

@Entity 
public class AssociationCandidatOffre implements Serializable { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Long idAssociation; 
    private String lettreMotivation; 
    private String tarifJournalier; 
    private Date dateDisponibilite; 

    @ManyToOne 
    private Candidat candidat; 

    @ManyToOne 
    private Offre offre; 
    @JsonIgnore 
    @XmlTransient 
    public Candidat getCandidat() { 
     return candidat; 
    } 
    @JsonSetter 
    public void setCandidat(Candidat candidat) { 
     this.candidat = candidat; 
    } 
    @JsonIgnore 
    @XmlTransient 
    public Offre getOffre() { 
     return offre; 
    } 
    @JsonSetter 
    public void setOffre(Offre offre) { 
     this.offre = offre; 
    } 

    //... getters/setters 
} 

das Problem ist, wenn ich rufe die api /offres mir ein json objekt zurückgeben bekomme ich diese fehlermeldung inste Anzeige:

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.***.Rekrute.entities.Offre["associationCandidatOffres"]); 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.***.Rekrute.entities.Offre["associationCandidatOffres"]) 

wenn ich @JsonIgnore im getAssocationCandidatOffres verwenden nicht, daß ich Fehler bekommen, aber ich möchte, dass die Assoziation im json Ergebnis als gut.

Normalerweise sollte dies keinen Fehler generieren, da ich @JsonIgnore in der anderen Seite der Beziehung, die getOffre() ist.

Wie kann ich dieses Problem lösen?

+0

Sind Sie sicher, dass die Liste 'getAssocationCandidatOffres' ausgefüllt ist? Beachten Sie, dass IDEs im Debug-Modus normalerweise eine Abfrage ausführen, um eine Liste mit verzögerten Ladungen im Hintergrund zu erhalten, wenn Sie die Liste erweitern. – dambros

Antwort

2

Sie können eine bidirektionale Beziehung einer Entität nicht in JSON konvertieren. Sie erhalten eine Endlosschleife.

JSON-Parser beginnt mit der Entity Offer und liest die zugehörige AssociationCandidatOffre über getAssociationCandidatOffres(). Für jeden AssociationCandidatOffre liest der JSON-Parser getOffre() und beginnt erneut. Der Parser weiß nicht, wann er enden muss.