java 无法写入JSON:无限递归(StackOverflowError)],带根本原因

8zzbczxx  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(341)

我正在使用postgresql开发一个spring Boot 应用程序。我有三个班级:User、从User继承的Medcin和Cabinet。在关系中,Medcin可以在一个Cabinet中,Cabinet可以有一个或多个Medcin

@Entity
@Getter
@Setter
@NoArgsConstructor

@AllArgsConstructor
@Table(name = "_user")

public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private String firstName;
    private String lastName;
    private int age;
    private String adress;
    private String cin;
    private String login;
    private String password;

}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Medecin extends User {
    private int inp;
    private String specialite;
    @ManyToOne
    private Cabinet cabinet;}
@Entity(name = "Cabinet")
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Cabinet {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "cabinet_id")
        private Long id;
        private String denomination;
        private String adresse;
        private int telephone;
        private double longitude;
        private double latitude;
        @OneToMany(mappedBy = "cabinet", cascade = CascadeType.ALL)
        private List<Medecin> medecins = new ArrayList<>();
    }

在邮件请求中创建一个我发送到身体的内阁

{
    "denomination": "Cabinet name",
    "adresse": "Cabinet address",
    "telephone": 123456789,
    "longitude": 0.0,
    "latitude": 0.0
}

然后我以同样的方式创建一个Medcin,如下所示:

{
    "email": "medecin@example.com",
    "password": "password",
    "firstName": "ww",
    "lastName": "Doe",
    "inp": 1234567,
    "specialite": "Cardiology",
    "cabinet": {
        "id": 1
    }
}

然后我做了一个获取请求来获取Medcin,但我得到的结果是这样的。我希望它返回我之前创建的Cabinet信息,但它为null。

{
    "id": 1,
    "firstName": "ww",
    "lastName": "Doe",
    "age": 0,
    "adress": null,
    "cin": null,
    "login": null,
    "password": "password",
    "inp": 1234567,
    "specialite": "Cardiology",
    "cabinet": {
        "id": 1,
        "denomination": null,
        "adresse": null,
        "telephone": 0,
        "longitude": 0.0,
        "latitude": 0.0,
        "medecins": null
    }
}

然后我执行get请求来获取文件柜,并得到以下错误:

Could not write JSON: Infinite recursion (StackOverflowError)] with root cause
gjmwrych

gjmwrych1#

由于您示例化的第一个对象是Cabinet,因此您应该使用此标记注解medecins属性:@JsonManagedReference
然后,第二个是Medecin,用这个标签注解cabinet属性:@JsonBackReference

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Medecin extends User {
    private int inp;
    private String specialite;
    @ManyToOne
    @JsonBackReference
    private Cabinet cabinet;
}

    @Entity(name = "Cabinet")
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Cabinet {
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "cabinet_id")
        private Long id;
        private String denomination;
        private String adresse;
        private int telephone;
        private double longitude;
        private double latitude;
        @OneToMany(mappedBy = "cabinet", cascade = CascadeType.ALL)
        @JsonManagedReference
        private List<Medecin> medecins = new ArrayList<>();
    }
zsbz8rwp

zsbz8rwp2#

如果@JsonIgnore注解在这种情况下不起作用,让我们尝试@JsonManagedReference@JsonBackReference
机柜:

@OneToMany(mappedBy = "cabinet", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Medecin> medecins = new ArrayList<>();

Medecin:

@ManyToOne
@JsonBackReference
private Cabinet cabinet;

也请检查一下:https://stackoverflow.com/a/43473926/10277150

相关问题