簡體   English   中英

JSON和Hibernate JPA的無限遞歸

[英]Infinite Recursion with JSON and Hibernate JPA

使用spring Boot,當嘗試將JPA對象轉換為JSON時,我不斷收到此錯誤:

nested exception is 
  `org.springframework.http.converter.HttpMessageNotWritableException:
     Could not write JSON: Infinite recursion (StackOverflowError); 
    nested exception is com.fasterxml.jackson.databind.
JsonMappingException: Infinite recursion (StackOverflowError)
 (through reference chain: interv.Entities.AppUser["projects"]->org.hibernate.collection.internal.PersistentBag[0]->interv.Entities.Project["appUser"]->interv.Entities.AppUser["projects"]->org.hibernate.collection.internal.Persis`

在堆棧溢出的一些解決方案之后,我通過添加注釋@JsonIgnoreProperties結束了,所以我的實體Project看起來像這樣:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Project implements Serializable{

        @Id @GeneratedValue
        private long id;
        private String intitule;
        private String description;

        @OneToMany(mappedBy = "project" , fetch = FetchType.EAGER)
       @Fetch(value = FetchMode.SUBSELECT)
        @JsonIgnoreProperties("contrats")
        private Collection<Contrat> contrats;

        @ManyToOne
        @JoinColumn(name = "Id_appUser")
        @JsonIgnoreProperties("appUser")
        private AppUser appUser;
 }

api寧靜看起來像這樣:

    import java.util.List;

@RestController
public class ProjectsController {

    @Autowired
    private ProjectRepo projectRepo;

    @RequestMapping(path = "/ListProjects", method = RequestMethod.GET)
    public List<Project> getProjects(){
        return projectRepo.findAll();
    }

我嘗試了其他注釋,但我不斷遇到相同的錯誤:

在ARC擴展中,我得到:

200 OK

解析JSON數據時出錯

JSON輸入意外結束

預先感謝您的幫助 :) 。

編輯:

文件AppUser.java

@Entity
@Data
@AllArgsConstructor @NoArgsConstructor
public class AppUser implements Serializable {

    @Id @GeneratedValue
    private Long id;
    @Column(unique = true)
    private String username;
    private String password;
    @ManyToMany(fetch = FetchType.EAGER)
    private Collection<AppRole> roles = new ArrayList<>();

    @OneToMany(mappedBy = "appUser" , fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private Collection<Project> projects = new ArrayList<>();


    @OneToMany(mappedBy = "appUser" , fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private Collection<Intervention> interventions = new ArrayList<>();

    @OneToMany(mappedBy = "appUser" , fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private Collection<Contrat> contrats = new ArrayList<>();

}

發生問題是因為生成JSON時存在無限循環。 您可以使用@JsonManagedReference和@JsonBackReference來解決無限遞歸。

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Project implements Serializable{

        @Id @GeneratedValue
        private long id;
        private String intitule;
        private String description;

        @OneToMany(mappedBy = "project" , fetch = FetchType.EAGER)
       @Fetch(value = FetchMode.SUBSELECT)
        @JsonIgnoreProperties("contrats")
        private Collection<Contrat> contrats;

        @ManyToOne
        @JoinColumn(name = "Id_appUser")
        @JsonBackReference
        private AppUser appUser;
 }

APPUSER

@Entity
@Data
@AllArgsConstructor @NoArgsConstructor
public class AppUser implements Serializable {

    @Id @GeneratedValue
    private Long id;
    @Column(unique = true)
    private String username;
    private String password;
    @ManyToMany(fetch = FetchType.EAGER)
    private Collection<AppRole> roles = new ArrayList<>();

    @OneToMany(mappedBy = "appUser" , fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    @JsonManagedReference
    private Collection<Project> projects = new ArrayList<>();


    @OneToMany(mappedBy = "appUser" , fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private Collection<Intervention> interventions = new ArrayList<>();

    @OneToMany(mappedBy = "appUser" , fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    private Collection<Contrat> contrats = new ArrayList<>();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM