2016-03-22 15 views
6

Ho un database MySQL e voglio recuperare alcuni dati come json.recupero dati dal database come json in spring boot

E ho un'entità Offre con la relazione @OneToMany con l'entità AssociationCandidatOffre.

e ho un api che calles questo metodo nel mio repository:

offreRepository.findAll(); 

Offre entità:

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

entità AssociationCandidatOffre:

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

il problema è quando chiamo the api /offres per restituirmi un oggetto json Ricevo questo messaggio di errore annuncio:

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"]) 

quando uso @JsonIgnore nel getAssocationCandidatOffres I dont ottenere gli eventuali errori ma voglio che l'associazione nel risultato JSON pure.

Normalmente, questo non dovrebbe generare alcun errore dal momento che ho @JsonIgnore nell'altro lato della relazione che è getOffre().

come posso risolvere questo problema?

+0

Sei sicuro che la lista 'getAssocationCandidatOffres' è popolata? Tenere presente che gli IDE in modalità di debug di solito eseguono una query per ottenere una lista di caricamento lenta in background quando si espande l'elenco. – dambros

risposta

2

Non è possibile convertire una relazione bidirezionale di un'enità in JSON. Hai un ciclo infinito.

JSON-Parser inizia con l'entità Offer e legge AssociationCandidatOffre associato tramite getAssociationCandidatOffres(). Per ogni AssociationCandidatOffre il parser JSON legge getOffre() e ricomincia. Il parser non sa quando deve finire.

Problemi correlati