2016-07-17 6 views
0

Ich habe Entitäten mit bidirektionaler Zuordnung zueinander. Aufruf der REST Http.GET-Anfrage, um alle Datensätze von db zu erhalten, ich erhalte StackOverflowException wegen unendlicher Rekursion. Ich habe versucht, @JsonIgnore, @JsonBackReference zusammen mit @JsonManageReference und @JsonIdentityInfo in verschiedenen Kombinationen zu verwenden, aber ohne positives Ergebnis. Ich erhalte immer noch den Fehler.Spring Boot - Einheit zu JSON - rekursiv

Spring Boot lädt mich Jackson in Version 2.6.6.

Hier ist meine BaseEntity:

@MappedSuperclass 
public class BaseEntity { 
    @Id 
    @GeneratedValue 
    private Long id; 
    private String createdBy; 
    private Date createdOn; 
    private String modifiedBy; 
    private Date modifiedOn; 
    public String description; 

    public BaseEntity() { 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getCreatedBy() { 
     return createdBy; 
    } 

    public void setCreatedBy(String createdBy) { 
     this.createdBy = createdBy; 
    } 

    public Date getCreatedOn() { 
     return createdOn; 
    } 

    public void setCreatedOn(Date createdOn) { 
     this.createdOn = createdOn; 
    } 

    public String getModifiedBy() { 
     return modifiedBy; 
    } 

    public void setModifiedBy(String modifiedBy) { 
     this.modifiedBy = modifiedBy; 
    } 

    public Date getModifiedOn() { 
     return modifiedOn; 
    } 

    public void setModifiedOn(Date modifiedOn) { 
     this.modifiedOn = modifiedOn; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 
} 

Erste Entity-Klasse:

@Entity 
public class Entry extends BaseEntity{ 

    private Date businessOperationDate; 

    @ManyToOne 
    private Version version; 

    @ManyToOne 
    private Status status; 

    @ManyToOne 
    @JsonManagedReference 
    private Account account; 

    public Entry() { 
    } 

    public Date getBusinessOperationDate() { 
     return businessOperationDate; 
    } 

    public void setBusinessOperationDate(Date businessOperationDate) { 
     this.businessOperationDate = businessOperationDate; 
    } 

    public Version getVersion() { 
     return version; 
    } 

    public void setVersion(Version version) { 
     this.version = version; 
    } 

    public Status getStatus() { 
     return status; 
    } 

    public void setStatus(Status status) { 
     this.status = status; 
    } 

    public Account getAccount() { 
     return account; 
    } 

    public void setAccount(Account account) { 
     this.account = account; 
    } 
} 

und die zweite:

@Entity 
public class Account extends BaseEntity{ 
    private String number; 

    @OneToMany(mappedBy = "account", fetch = FetchType.EAGER) 
    @JsonBackReference 
    private List<Entry> entries; 

    @ManyToMany(mappedBy = "accounts") 
    private List<Project> projects; 

    public Account() { 
    } 

    public String getNumber() { 
     return number; 
    } 

    public void setNumber(String number) { 
     this.number = number; 
    } 

    public List<Entry> getEntries() { 
     return entries; 
    } 

    public void setEntries(List<Entry> entries) { 
     this.entries = entries; 
    } 

    public List<Project> getProjects() { 
     return projects; 
    } 

    public void setProjects(List<Project> projects) { 
     this.projects = projects; 
    } 
} 

Hier können Sie einen Teil des Ergebnisses aus Http erhielt finden. GET-Anfrage:

[{"id":1,"createdBy":null,"createdOn":null,"modifiedBy":null,"modifiedOn":null,"description":"pierwszy zapis","businessOperationDate":null,"version":null,"status":null,"account":{"id":1,"createdBy":null,"createdOn":null,"modifiedBy":null,"modifiedOn":null,"description":"pierwszy projekt","number":null,"entries": 
[{"id":1,"createdBy":null,"createdOn":null,"modifiedBy":null,"modifiedOn":null,"description":"pierwszy zapis","businessOperationDate":null,"version":null,"status":null,"account":{"id":1,"createdBy":null,"createdOn":null,"modifiedBy":null,"modifiedOn":null,"description":"pierwszy projekt","number":null,"entries": 
[{"id":1,"createdBy":null,"createdOn":null,"modifiedBy":null,"modifiedOn":null,"description":"pierwszy zapis","{"timestamp":1468778765328,"status":200,"error":"OK","exception":"org.springframework.http.converter.HttpMessageNotWritableException","message": 
"Could not write content: Infinite recursion (StackOverflowError) (through reference chain: com.test.test2.core.dto.AccountDto[\"entries\"]-> 
java.util.ArrayList[0]->com.test.test2.core.dto.EntryDto[\"account\"]->com.test.test2.core.dto.AccountDto[\"entries\"]-> 
java.util.ArrayList[0]->com.test.test2.core.dto.EntryDto[\"account\"]->com.test.test2.core.dto.AccountDto[\"entries\"]->java.util.ArrayList[0]->com.test.test2.core.dto.EntryDto[\"account\"]->com.test.test2.core.dto.AccountDto[\"entries\"]-> 
java.util.ArrayList[0]->com.test.test2.core.dto.EntryDto[\"account\"]- 

pom.xml-Datei:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.test</groupId> 
    <artifactId>test2</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>test2</name> 
    <description>test2</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.5.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <!--<dependency>--> 
      <!--<groupId>org.springframework.boot</groupId>--> 
      <!--<artifactId>spring-boot-starter-security</artifactId>--> 
     <!--</dependency>--> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <scope>runtime</scope> 
      <version>9.4-1201-jdbc41</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <!-- model mapper --> 
     <dependency> 
      <groupId>ma.glasnost.orika</groupId> 
      <artifactId>orika-core</artifactId> 
      <version>1.4.6</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

Bitte raten, was ich falsch mache. Ich möchte in einem Ergebnis nur eine Ebene erhalten, z.B. Aufruf von getAll() für den Eintrag, ich möchte alle Einträge mit Informationen erhalten, welche Konto verwandt ist, und im Gegenteil einmal getAll() für Konto aufrufen.

+0

Bitte Veröffentlichen Sie Ihre pom.xml, überprüfen Sie die json-Antwort in der Spring-Boot-Eigenschaftendatei aus ihrer Dokumentation. Außerdem haben Sie in Ihrer pom.xml einen Ausschluss für den Jackson Bind-Fehler hinzugefügt. –

+0

Ich habe pom.xml hinzugefügt. Danke für deine Unterstützung. – kbysiec

Antwort

1

Suche i mehrmals für diesen Fehler, aber ich kann nichts treffen erhalte ich diese Fehlerfall und ich korrigieren, indem Sie die Anmerkung @JsonIgnore in irgendeiner Beziehung Mapping,

dieses Beispiel ist

@ManyToMany(mappedBy = "accounts") 
    @JsonIgnore 
    private List<Project> projects;