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();
@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
}
@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 ?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With