该应用程序启动正常,但是当我尝试执行get请求时,我收到以下错误:
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle 我认为我自我引用的方式有问题。请帮帮忙。提前谢谢。
下面是我的sql:
CREATE TABLE tbl_person (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
parent_id BIGINT(20),
primary key (id));
CREATE SEQUENCE person_seq START WITH 1 INCREMENT BY 1;这是我的模型:
@Entity
@Data
@Table(name="tbl_person")
public class Person {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE,generator = "person_seq")
@SequenceGenerator(name="person_seq",allocationSize = 1)
@Column(name="id",insertable =false,nullable = false,updatable = false)
private Long id;
@Column(name="name")
private String name;
@Column(name="address")
private String address
@OneToOne
@JoinColumn(name="parent_id")
@JsonManagedReference
private Person person;
}发布于 2020-12-17 15:33:45
错误消息来自jackson。所以它在转换为java对象或返回到json/xml时失败,而不是将其写入数据库。
问题是,生成的JSON对象可能包含另一个Person-json,它可能引用同一个person。这将创建一个无限循环。
会产生这样的结果:
{
"id": 1,
"name": "Example",
"address": "example-address",
"person": {
"id": 1,
"name": "Example",
"address": "example-address",
"person": {
"id": 1,
"name": "Example",
"address": "example-address",
"person": {
...
}
}
}
}https://stackoverflow.com/questions/65336205
复制相似问题