Ho trovato una gran quantità di risorse su questo, come questo - Infinite Recursion with Jackson JSON and Hibernate JPA issue. Ho provato per implementare tutti i vari suggerimenti descritti qui (incluso base @ JsonIgnore), ma senza risultato. Non riesco a ottenere altro che errori di ricorsione infiniti, indipendentemente da quello che cerco. Penso di avere un setup abbastanza simile/tipico, ma ovviamente con qualcosa di sbagliato poiché nonostante utilizzo le annotazioni @JsonManagedReference, @JsonBackReferencere e @JsonIdentityInfo, continuo a ricevere l'errore.Ricorsione infinita con Jackson JSON e Hibernate JPA problema (ancora un altro)
Le mie tabelle sono "exchange" e "stock", con un numero molto elevato tra loro, e sono stato testato da ExchangeEndpoint. Ho confermato che se rimuovo completamente "stock" dall'entità "exchange", il servizio funziona correttamente, ma per qualche motivo, le annotazioni JSON non sembrano avere alcun effetto. Di seguito è riportato ciò che I pensa che la soluzione sia basata sulla seconda (ma più popolare) risposta nel già citato Infinite Recursion with Jackson JSON and Hibernate JPA issue.
Exchange.java
@Entity
@Table(name = "exchange", schema = "public")
@XmlRootElement
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Exchange implements java.io.Serializable {
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "exchange")
@JsonManagedReference
public Set<Stock> getStocks() {
return this.stocks;
}
...
Stock.java
@Entity
@Table(name = "stock", schema = "public")
@XmlRootElement
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Stock implements java.io.Serializable {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "exchangeid", nullable = false)
@JsonBackReference
@JsonIgnore
public Exchange getExchange() {
return this.exchange;
}
...
ExchangeEndpoint.java
@Stateless
@Path("/exchanges")
public class ExchangeEndpoint {
@PersistenceContext(unitName = "postgresql-ss4")
private EntityManager em;
...
@GET
@Produces("application/json")
public List<Exchange> listAll(@QueryParam("start") Integer startPosition,
@QueryParam("max") Integer maxResult) {
TypedQuery<Exchange> findAllQuery = em
.createQuery(
"SELECT DISTINCT e "
+ "FROM Exchange e "
+ "LEFT JOIN FETCH e.stocks "
+ "ORDER BY e.exchangeid",
Exchange.class);
if (startPosition != null) {
findAllQuery.setFirstResult(startPosition);
}
if (maxResult != null) {
findAllQuery.setMaxResults(maxResult);
}
final List<Exchange> results = findAllQuery.getResultList();
return results;
}
edit:
alcuni dei output di errore per essere sicuro che' Non mi fraintendere g qualcosa;
15: 35: 16.406 ERRORE [org.jboss.resteasy.resteasy_jaxrs.i18n] (HTTP/0.0.0.0: 8080-1) RESTEASY000100: Impossibile eseguire GET /scambi /: org.jboss. resteasy.spi.WriterException: org.codehaus.jackson.map.JsonMappingException: Infinita ricorsione (StackOverflowError) (attraverso la catena di riferimenti: net.hb.forge2RestServices.model.Exchange ["stocks"] -> org.hibernate.collection .internal.PersistentSet [0] -> net.hb.forge2RestServices.model.Stock ["exchange"] -> net.hb.forge2RestServices.model.Exchange ["stocks"] -> org.hibe ... . .. Scambio ["scorte"] -> org.hibernate.collection.internal.P ersistentSet [0] -> net.hb.forge2RestServices.model.Stock ["exchange"] -> net.hb.forge2RestServices.model.Exchange ["stocks"]) all'indirizzo org.jboss.resteasy.core.ServerResponse.writeTo (ServerResponse.java:262)
Per favore fatemi sapere quali informazioni aggiuntive posso fornire per aiutare a spiegare la mia debacle. Tiy.
Ho provato ad aggiungere l'annotazione JsonIgnore in un paio di modi diversi, ma ancora non sembra niente si sta comportando in modo diverso. Ho aggiornato la domanda originale con uno di loro, dove l'ho appena attaccato, ma ho anche provato a sostituire JsonBackReference e spostare le annotazioni nella definizione del campo (invece del getter), ma mi sembra di ottenere gli stessi risultati. C'è un modo in cui queste annotazioni potrebbero essere negate? – swv