有关数据库的详细信息: folder_Meta_Data有字段ID,parentFolderId,其中id只有folderid就像主键一样,parentFolderId表示id。
@Entity
@Table(name = "folder_Meta_Data")
@Data
@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id",scope=FolderMetaData.class)
public class FolderMetaData implements Serializable {
@Id
@SequenceGenerator(name = "seq-gen", initialValue = 1)
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "seq-gen")
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "projectId", nullable = false)
private Long projectId;
@Column(name = "mgId", nullable = false)
private Long mgId;
@Column(name = "folderRoot", length = 50)
private String folderRoot;
@Column(name = "folderExtention", length = 50)
private String folderExtention;
@Column(name = "folderName", nullable = false, length = 255)
private String folderName;
@Column(name = "createdBy", nullable = false)
private Long createdBy;
@Column(name = "createdTime", nullable = false)
private Date createdTime;
@Column(name = "versionOCC")
private Long versionOCC;
@Column(name = "subProjId", nullable = false)
private Long subProjId;
@Column(name = "modifiedBy")
private Long modifiedBy;
@Column(name = "modifiedTime", length = 50)
private Date modifiedTime;
@Column(name = "parentFolderId")
private Long parentFolderId;
@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "parentFolderId", insertable = false, updatable = false)
@JsonBackReference
// @JsonIgnore
// child role
private FolderMetaData folder;
@OneToMany(mappedBy = "folder")
// parent role
@JsonManagedReference
private Set<FolderMetaData> folderList;
// Getter and Setter methods
}
JSON树响应:
The response i am getting is as below
[
{
"id": 1,
"projectId": 125,
"mgId": 34512,
"folderRoot": null,
"folderExtention": null,
"folderName": "XY1",
"createdBy": 12,
"createdTime": "2018-03-16",
"versionOCC": null,
"subProjId": 561565,
"modifiedBy": null,
"modifiedTime": null,
"parentFolderId": null,
"folderList": []
},
{
"id": 2,
"projectId": 125,
"mgId": 34512,
"folderRoot": null,
"folderExtention": null,
"folderName": "XY2",
"createdBy": 12,
"createdTime": "2018-03-16",
"versionOCC": null,
"subProjId": 561565,
"modifiedBy": null,
"modifiedTime": null,
"parentFolderId": null,
"folderList": [
{
"id": 3,
"projectId": 125,
"mgId": 34512,
"folderRoot": null,
"folderExtention": null,
"folderName": "XY2",
"createdBy": 12,
"createdTime": "2018-03-16",
"versionOCC": null,
"subProjId": 561565,
"modifiedBy": null,
"modifiedTime": null,
"parentFolderId": 2,
"folderList": [
{
"id": 4,
"projectId": 125,
"mgId": 34512,
"folderRoot": null,
"folderExtention": null,
"folderName": "XY2",
"createdBy": 12,
"createdTime": "2018-03-16",
"versionOCC": null,
"subProjId": 561565,
"modifiedBy": null,
"modifiedTime": null,
"parentFolderId": 3,
"folderList": [
{
"id": 5,
"projectId": 125,
"mgId": 34512,
"folderRoot": null,
"folderExtention": null,
"folderName": "XY2",
"createdBy": 12,
"createdTime": "2018-03-16",
"versionOCC": null,
"subProjId": 561565,
"modifiedBy": null,
"modifiedTime": null,
"parentFolderId": 4,
"folderList": []
}
]
}
]
}
]
},
3,
4,
5
]
在服务类方法中,我在下面调用方法来检索所有记录。由于它是Jackson api,所有获取的记录都作为父母和孩子的关系填充到列表中。记录得到json树。
List<FolderMetaData> list=folderRepository.findAll();
问题是: 在FolderMetaData pojo中使用@JsonBackReference和@JsonManagedReference之后。
The tree is coming correctly. but in the bottom of this json array response, I am getting dirty data which are ids of subtrees. if anyone help me to fix this issue, that would be great.
如下所示
,
3,
4,
5
答案 0 :(得分:0)
我认为您的问题是lombok @Data
,它会生成引用您自引用属性的toString
,equals
和hascode
方法。这会产生stackoverflow错误,这与序列化无关,但根本原因是相同的(自引用)。请按照文档中的说明使用lombok Exclude或使用@Getter @Setter
并自行实施此方法。
答案 1 :(得分:0)
问题是你从数据库中获取所有数据, 你可以在这里查看Spring boot + (JPA) - Category hierarchy - recursively traverse,他有同样的问题