Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving data from database as json in spring boot

I have a MySQL database and I want to retrieve some data as json.

And I have an entity Offre wich has @OneToMany relation with the AssociationCandidatOffre entity.

and I have an api which calles this method in my repository :

offreRepository.findAll();

Offre entity :

@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 entity :

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

the problem is when I call the api /offres to return me a json object I get this error message instead :

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

when I use @JsonIgnore in the getAssocationCandidatOffres I dont get any errors but I want that association in the json result as well.

Normally, this shouldn't generate any error since I have @JsonIgnore in the other side of the relation which is getOffre().

how can I solve this problem ?

like image 715
Renaud is Not Bill Gates Avatar asked Mar 22 '16 13:03

Renaud is Not Bill Gates


People also ask

How does Spring boot convert object to JSON?

When Jackson is on the classpath an ObjectMapper bean is automatically configured. The spring-boot-starter-json is pulled with the spring-boot-starter-web . In Spring objects are automatically convered to JSON with the Jackson library. Spring can be configured to convert to XML as well.


1 Answers

You can't convert a bidirectional relation of an enitity to JSON. You get an endless loop.

JSON-Parser starts with the entity Offer and reads the associated AssociationCandidatOffre via getAssociationCandidatOffres(). For every AssociationCandidatOffre the JSON-Parser read getOffre() and starts again. The parser don't know when he must end.

like image 149
UKoehler Avatar answered Sep 30 '22 18:09

UKoehler