Spring Boot - entity to json - recursive
<p>I have entities with bidirectional mapping to each other. Calling REST Http.GET request to get all records from db, I am receiving StackOverflowException due to infinite recursion. I was trying to use @JsonIgnore, @JsonBackReference together with @JsonManageReference and @JsonIdentityInfo in different combinations, but with no positive result. I am still receiving the error.</p>

<p>Spring Boot loads me jackson in version 2.6.6.</p>

<p>Here is my BaseEntity:</p>

<pre><code>@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;
    }
}
</code></pre>

<p>First Entity class:</p>

<pre><code>@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;
    }
}
</code></pre>

<p>and the second one:</p>

<pre><code>@Entity
public class Account extends BaseEntity{
    private String number;

    @OneToMany(mappedBy = "account", fetch = FetchType.EAGER)
    @JsonBackReference
    private List&lt;Entry&gt; entries;

    @ManyToMany(mappedBy = "accounts")
    private List&lt;Project&gt; projects;

    public Account() {
    }

    public String getNumber() {
        return number;
    }

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

    public List&lt;Entry&gt; getEntries() {
        return entries;
    }

    public void setEntries(List&lt;Entry&gt; entries) {
        this.entries = entries;
    }

    public List&lt;Project&gt; getProjects() {
        return projects;
    }

    public void setProjects(List&lt;Project&gt; projects) {
        this.projects = projects;
    }
}
</code></pre>

<p>Here you can find part of result received from Http.GET request:</p>

<pre><code>[{"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\"]-&gt;
java.util.ArrayList[0]-&gt;com.test.test2.core.dto.EntryDto[\"account\"]-&gt;com.test.test2.core.dto.AccountDto[\"entries\"]-&gt;
java.util.ArrayList[0]-&gt;com.test.test2.core.dto.EntryDto[\"account\"]-&gt;com.test.test2.core.dto.AccountDto[\"entries\"]-&gt;java.util.ArrayList[0]-&gt;com.test.test2.core.dto.EntryDto[\"account\"]-&gt;com.test.test2.core.dto.AccountDto[\"entries\"]-&gt;
java.util.ArrayList[0]-&gt;com.test.test2.core.dto.EntryDto[\"account\"]-
</code></pre>

<p>pom.xml file:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.test&lt;/groupId&gt;
    &lt;artifactId&gt;test2&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;

    &lt;name&gt;test2&lt;/name&gt;
    &lt;description&gt;test2&lt;/description&gt;

    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;1.3.5.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;

    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;!--&lt;dependency&gt;--&gt;
            &lt;!--&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;--&gt;
            &lt;!--&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;--&gt;
        &lt;!--&lt;/dependency&gt;--&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
            &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
            &lt;version&gt;9.4-1201-jdbc41&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;

        &lt;!-- model mapper --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;ma.glasnost.orika&lt;/groupId&gt;
            &lt;artifactId&gt;orika-core&lt;/artifactId&gt;
            &lt;version&gt;1.4.6&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;
</code></pre>

<p>Please advise, what I am doing wrong. I wish to receive in a result only one level, e.g. calling getAll() for entry, I wish to receive all entries with information which account is related, and in opposite once calling getAll() for account.</p>