繁体   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