上下文
我正在尝试使用SpringBoot,Hateeoas创建一个API,将数据存储到H2数据库中并通过JPA对其进行管理。
我有2个实体:Mission
和User
。
几个用户被分配了一个任务。并且可以将用户分配给不同的任务。
我使用@ManyToMany
批注创建关联表。
目标
我的目标是创建一些请求:
IP/missions/123456/users
:获取分配给任务123456
的所有用户IP/missions/456789
:已分配用户到任务456789
代码
实体
@Entity
public class Mission {
@Id
private String missionid;
private String missionname;
private Date start;
private Date end;
@ManyToMany(mappedBy = "missions")
@JoinTable(name = "missions_users",
joinColumns = @JoinColumn(name = "missionid"),
inverseJoinColumns = @JoinColumn(name = "userid")
)
private Set<User> users = new HashSet<>();
private int status;
public Mission() {
}
public Mission(String name, Date start, Date end) {
this.namemission = name;
this.start = start;
this.end = end;
this.status = 0;
}
}
@Entity
public class User {
@Id
private String userid;
private String nameuser;
@ManyToMany
private Set<Mission> missions = new HashSet<>();
public User() {
}
public User(String name) {
this.nameuser = name;
}
}
资源
@RepositoryRestResource(collectionResourceRel = "mission")
public interface MissionResource extends JpaRepository<Mission, String> {
}
@RepositoryRestResource(collectionResourceRel = "user")
public interface UserResource extends JpaRepository<User, String> {
}
RestController
@RestController
@RequestMapping(value = "/missions", produces = MediaType.APPLICATION_JSON_VALUE)
@ExposesResourceFor(Mission.class)
public class MissionRepresentation {
private final MissionResource missionResource;
private final UserResource userResource;
public MissionRepresentation(MissionResource missionResource, UserResource userResource) {
this.missionResource = missionResource;
this.userResource = userResource;
}
// mapping
}
用于填充数据库的SQL脚本
INSERT INTO mission (missionid, namemission, start, end, status) VALUES ('de7d9052-4961-4b4f-938a-3cd12cbe1f82', 'mission 1', '2019-02-11', '2019-02-13', 0)
INSERT INTO mission (missionid, namemission, start, end, status) VALUES ('425e7701-02c6-4de3-9333-a2459eece1c8', 'mission 2', '2019-02-10', '2019-02-15', 0)
INSERT INTO user (userid, nameuser) VALUES ('0dee5423-6bd9-4014-a690-0993b3cb5f3b', 'user 1')
INSERT INTO user (userid, nameuser) VALUES ('8e70ca41-8766-4188-bf37-3d9a0aae941d', 'user 2')
INSERT INTO user (userid, nameuser) VALUES ('75cb846a-3a7b-4116-8d8a-7778916dff8c', 'user 3')
INSERT INTO missions_users(missionid, userid) VALUES ('de7d9052-4961-4b4f-938a-3cd12cbe1f82', '0dee5423-6bd9-4014-a690-0993b3cb5f3b')
INSERT INTO missions_users(missionid, userid) VALUES ('de7d9052-4961-4b4f-938a-3cd12cbe1f82', '8e70ca41-8766-4188-bf37-3d9a0aae941d')
INSERT INTO missions_users(missionid, userid) VALUES ('425e7701-02c6-4de3-9333-a2459eece1c8', '8e70ca41-8766-4188-bf37-3d9a0aae941d')
INSERT INTO missions_users(missionid, userid) VALUES ('425e7701-02c6-4de3-9333-a2459eece1c8', '75cb846a-3a7b-4116-8d8a-7778916dff8c')
结果
当前,我尝试执行GET
个请求,但是将来我将不得不执行POST
/ PUT
/ ...个请求。
请求:http://localhost:8082/mission/de7d9052-4961-4b4f-938a-3cd12cbe1f82
{
"missionid": "de7d9052-4961-4b4f-938a-3cd12cbe1f82",
"namemission": "mission 1",
"users": [
{},
{}
],
"status": 0,
"start": "2019-02-10T23:00:00.000+0000",
"end": "2019-02-12T23:00:00.000+0000"
}
请求:http://localhost:8082/mission/de7d9052-4961-4b4f-938a-3cd12cbe1f82
{
"missionid": "de7d9052-4961-4b4f-938a-3cd12cbe1f82",
"namemission": "mission 1",
"users": [
"user 1",
"user 2"
],
"status": 0,
"start": "2019-02-10T23:00:00.000+0000",
"end": "2019-02-12T23:00:00.000+0000"
}
请求:http://localhost:8082/mission/de7d9052-4961-4b4f-938a-3cd12cbe1f82/users
{
"user 1",
"user 2"
}
我不知道如何编写方法来仅获取用户名...
@GetMapping(value = "/{missionId}/users")
public ResponseEntity<?> getUsersByMission(@PathVariable("missionId") String missionId) {
Mission mission = this.missionResource.findById(missionId).get();
return new ResponseEntity<>(????, HttpStatus.OK);
}
一种解决方案是使用String列表而不是User列表?
结论
有人可以帮助我取得预期的结果吗?谢谢。
编辑1:@LppEdd答案的结果
[
{
"userid": "0dee5423-6bd9-4014-a690-0993b3cb5f3b",
"nameuser": "user 1",
"missions": []
},
{
"userid": "8e70ca41-8766-4188-bf37-3d9a0aae941d",
"nameuser": "user 2",
"missions": []
}
]
答案 0 :(得分:1)
实际上应该很容易。你快过去了
JPA将为您处理User
对象的创建。只需将您的Set<User>
暴露给外界即可。
public class Mission {
...
public Set<User> getUsers() {
return users;
}
public void setUsers(final Set<User> users) {
this.users = users;
}
...
}
然后在@RestController
内,保持住并返回Set
。
public ResponseEntity<List<String>> getUsersByMission(@PathVariable("missionId") String missionId) {
final Optional<Mission> mission = this.missionResource.findById(missionId);
if (mission.isEmpty()) {
// Handle the "mission not found" case
}
final List<String> names =
mission.get().getUsers()
.stream()
.map(user -> user.getNameUser())
.collect(Collectors.toList());
return ResponseEntity.ok().body(names);
}
要处理双向@ManyToMany
关联,您需要指定mappedBy
属性,该属性将指向Mission#users
字段。
@ManyToMany(mappedBy = "users")
private Set<Mission> missions = new HashSet<>();