gpt4 book ai didi

java - 在 Spring Boot 中,我无法通过 REST Controller 上的属性获取多对多的 JSON

转载 作者:行者123 更新时间:2023-12-01 17:55:23 25 4
gpt4 key购买 nike

我有一个 SpringBoot REST API,其中一个或多个用户属于一个或多个团队(Equipe),但每个团队都有一个角色。

当我请求 UserRepository.findAll() 时,我想获取所有用户及其团队和角色。但是当我请求 TeamRepository.findAll() 时,我想获取我的所有团队及其用户和角色。

但我不知道如何获得双向请求。我尝试使用 @jsonmanagementreference 和 @jsonbackreference 但它只允许我从用户那里获取团队,而不是相反。

目前我有

[
{
"id": 1,
"prenom": "TestPrenom1",
"nom": "TestNom1",
"roleUserEquipe": []
}
{
"id": 2,
"prenom": "TestPrenom2",
"nom": "TestNom2",
"roleUserEquipe": []
}
]

但我想要的是

[
{
"id": 1,
"prenom": "TestPrenom1",
"nom": "TestNom1",
"roleUserEquipe": [
{
"role": "Role1",
"team": {
"id": 1,
"nom": "Team1"
}
}
]
}
{
"id": 2,
"prenom": "TestPrenom2",
"nom": "TestNom2",
"roleUserEquipe": [
{
"role": "Role2",
"team": {
"nom": "Team1"
}
}
]
}
]

当我请求我的团队时,我有

[
{
"id": 1,
"nom": "Team1",
"roleUserEquipe": [
{}
]
},
{
"id": 2,
"nom": "Team2",
"roleUserEquipe": [
{}
]
}
]

但是我愿意

[
{
"id": 1,
"nom": "Team1",
"roleUserEquipe": [
{
"role": "Role1",
"user": {
"id": 1,
"prenom": "TestPrenom1",
"nom": "TestNom1",
}
},
{
"role": "Role2",
"user": {
"id": 2,
"prenom": "TestPrenom2",
"nom": "TestNom2",
}
}
]
},
{
"id": 2,
"nom": "Team2",
"roleUserEquipe": []
}
]

我的用户.java


@Entity
@JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
public class User implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String prenom;
private String nom;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<RoleUserEquipe> roleUserEquipe = new HashSet<>();

//GETTERS AND SETTERS AND CONSTRUCTORS

}

我的Equipe.java(团队)


@Entity
@JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Equipe implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String nom;

@OneToMany(mappedBy = "equipe", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<RoleUserEquipe> roleUserEquipe = new HashSet<>();

//GETTERS AND SETTERS AND CONSTRUCTORS

}

我的RoleUserEquipe.java

@Entity
public class RoleUserEquipe implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "equipe_id")
private Equipe equipe;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;

private String role;

//GETTERS AND SETTERS AND CONSTRUCTORS

}

编辑:
调试时,子对象没问题:它们包含我需要的值。
仅当返回 JSON 响应时,它们才会丢失其值

最佳答案

我不确定这是否有效,但请尝试删除以下位置的新关键字:

 @OneToMany(mappedBy = "equipe", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<RoleUserEquipe> roleUserEquipe = new HashSet<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<RoleUserEquipe> roleUserEquipe = new HashSet<>();

  @OneToMany(mappedBy = "equipe", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<RoleUserEquipe> roleUserEquipe;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<RoleUserEquipe> roleUserEquipe;

关于java - 在 Spring Boot 中,我无法通过 REST Controller 上的属性获取多对多的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60726579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com