英文:
How to assign a list of list of things to another list of things using an endpoint
问题
Sure, here's the translated code portion:
我有一个端点,用于将一组*角色*分配给单个*用户*。
@PutMapping(value = "/roles/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
public UserDto modifyRoles(
@ApiParam(value = "用户ID", required = true) @PathVariable("id") Long id,
@ApiParam(value = "ID角色列表", required = true) @RequestBody List<Long> roles)
throws someExceptions {
return serviceClass.modifyUserRoles(id, roles);
}
我想要接收一组*用户*以及一组*角色*,以便为每个用户分配每个角色,但我不确定如何做以及是否应该通过@PathVariable发送用户列表,或者是否有更好的方法。这是我尝试过的:
@PutMapping(value = "/roles/{usersId}", consumes = MediaType.APPLICATION_JSON_VALUE)
public List<UserDto> modifyMultipleRoles(
@ApiParam(value = "用户ID列表", required = true) @PathVariable("id") List<Long> ids,
@ApiParam(value = "ID角色列表", required = true) @RequestBody List<Long> roles)
throws someExceptions {
return serviceClass.newMethodThatAssignEachRoleToEachUser(ids, roles);
}
这是最佳方法吗?这个方法是否有效?
谢谢
**更新响应**
这是我刚刚尝试的内容
List<Long> usersId = objectDto.getUsersId();
List<Long> rolesToAdd = objectDto.getRolesToAdd();
List<Long> rolesToDelete = objectDto.getRolesToRemove();
List<Usuario> users = usersId.stream()
.map(userId -> this.findById(userId))
.collect(Collectors.toList());
return users.stream().
// ????
If you have any more specific translation needs or questions, please feel free to ask.
英文:
I have an endpoint which assign a list of Roles to a single user.
@PutMapping(value = "/roles/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
public UserDto modifyRoles(
@ApiParam(value = "User ID", required = true) @PathVariable("id") Long id,
@ApiParam(value = "List of ID roles", required = true) @RequestBody List<Long> roles)
throws someExceptions {
return serviceClass.modifyUserRoles(id, roles);
}
I want to receive a list of Users aswell a list of Roles so I can assign every role to each user, but i'm not sure how to do it and if I should send the list of users via @PathVariable, or if I there's a better way to do it. This is what I've tried:
@PutMapping(value = "/roles/{usersId}", consumes = MediaType.APPLICATION_JSON_VALUE)
public List<UserDto> modifyMultipleRoles(
@ApiParam(value = "List of User ID", required = true) @PathVariable("id") List<Long> ids,
@ApiParam(value = "List of ID roles", required = true) @RequestBody List<Long> roles)
throws someExceptions {
return serviceClass.newMethodThatAssignEachRoleToEachUser(ids, roles);
}
Is this the best way to do it? Would this even work?
Thank you
Update response
This is what I've tried right now
List<Long> usersId=objectDto.getUsersId();
List<Long> rolesToAdd = objectDto.getRolesToAdd();
List<Long> rolesToDelete = objectDto.getRolesToRemove();
List<Usuario> users = usersId.stream()
.map(userId -> this.findById(userId))
.collect(Collectors.toList());
return users.stream().
// ????
Is that the way to proceed?
答案1
得分: 1
你需要创建一个请求类,如下所示,使用 @RequestBody 注解:
{
"users": ["user1", "user2"],
"roles": ["role1", "role2"]
}
public class RolesRequestBody {
public List<String> users;
public List<String> roles;
}
英文:
You need to make a request class like below as @RequestBody
{
"users": ["user1", "user2"],
"roles": ["role1", "role2"]
}
public class RolesRequestBoday {
public List<String> users;
public List<String> roles;
}
答案2
得分: 1
以下是您要翻译的内容:
要做的简单方法是将这些列表包装在相同的对象中,并将此对象用作requestBody参数:
public class UsersRolsRequest () {
private List<Long> users;
private List<Long> roles;
}
最终结果应该如下:
@PutMapping(value = "/roles/{usersId}", consumes = MediaType.APPLICATION_JSON_VALUE)
public List<UserDto> modifyMultipleRoles(@ApiParam(value = "包含用户ID列表和角色ID列表的对象", required = true) @RequestBody UsersRolsRequest request) throws someExceptions {
return serviceClass.newMethodThatAssignEachRoleToEachUser(ids, roles);
}
请求JSON将如下所示:
{
"users": [1,2,3],
"roles": [1,1,2]
}
英文:
The easy way to do it is to wrap those list with the same object and use this objest as requestBody param:
public class UsersRolsRequest (){
private List<Long> users;
private List<Long> rols;
}
The final result shoul be like this:
@PutMapping(value = "/roles/{usersId}", consumes = MediaType.APPLICATION_JSON_VALUE)
public List<UserDto> modifyMultipleRoles(@ApiParam(value = "Object with the list of ID
users and the List of ID roles", required = true) @RequestBody UsersRolsRequest
request)throws someExceptions {
return serviceClass.newMethodThatAssignEachRoleToEachUser(ids, roles);
}
The request json will be like:
{
"users": [1,2,3],
"rols": [1,1,2]
}
答案3
得分: 1
-
您目前的解决方案不会起作用,因为您无法将列表用作路径参数。所以您想要一个表示
用户
和角色
列表的单个有效负载 -
然后,首要问题是您是否希望将相同的角色修改给每个用户,还是您希望为每个用户修改不同的角色?根据答案,有效负载可以是以下之一:
{
"users": ["user1", "user2"],
"roles": ["role1", "role2"]
}
或者
[
{
"id": "user1",
"roles": ["role1", "role2"]
},
{
"id": "user2",
"roles": ["role2", "role3"]
}
]
-
一旦您做出了决定,第二个问题涉及“修改”角色的含义。这是否意味着添加一些角色并删除其他一些角色,还是意味着用新的角色集替换用户当前的角色?
- Http
Patch
变种 1 - Http
Patch
变种 2 - Http
Put
变种 1 - Http
Put
变种 2
- Http
{
"users": ["user1", "user2"],
"rolesToAdd": ["role1", "role2"],
"rolesToRemove": ["role3"]
}
[
{
"id": "user1",
"rolesToAdd": ["role1"],
"rolesToRemove": ["role2"],
},
{
"id": "user2",
"rolesToAdd": ["role2", "role3"],
"rolesToRemove": ["role4"],
}
]
{
"users": ["user1", "user2"],
"roles": ["role1", "role2"]
}
[
{
"id": "user1",
"roles": ["role1", "role2"]
},
{
"id": "user2",
"roles": ["role2", "role3"]
}
]
- 然后,只需创建用于表示有效负载的Java对象,并相应地进行持久化
更新
- 您可以创建
ModifyUsersRoleRequest
类,其中包含这3个字段的getter和setter方法。然后,您可以将其用作@RequestBody
。一旦进入方法内部,进行spring-data-jpa
存储库调用以获取具有这些用户ID的所有用户,然后您将获得一个用户列表。然后,对于每个用户,添加新角色并删除您想要删除的角色。
英文:
-
Your current solution won't won't work as you cannot have list as path parameter. So you want to have single payload that represents list of
user
s androle
s -
And then the fist question is do you want to modify the same roles to every user or you want to modify different roles to each user?. Depending on that answer, the payload could be one of the variance
{
"users": ["user1", "user2"],
"roles": ["role1", "role2"]
}
or
[
{
"id: "user1"
"roles": ["role1", "role2"]
},
{
"id: "user2"
"roles": ["role2", "role3"]
},
]
-
Once you have made that decision, second question is about the meaning of
modifying
roles. Does that mean adding some roles and removing some other roles or does it mean replacing the user's current roles with new set of roles?- Http
Patch
variance 1 - Http
Patch
variance 2 - Http
Put
variance 1 - Http
Put
variance 2
- Http
{
"users": ["user1", "user2"],
"rolesToAdd": ["role1", "role2"]",
"rolesToRemove": ["role3"]
}
[
{
"id: "user1"
"rolesToAdd": ["role1"],
"rolesToRemove": ["role2"],
},
{
"id: "user2"
"rolesToAdd": ["role2", "role3"],
"rolesToRemove": ["role4"],
},
]
{
"users": ["user1", "user2"],
"roles": ["role1", "role2"]",
}
[
{
"id: "user1"
"roles": ["role1", "role2"]
},
{
"id: "user2"
"roles": ["role2", "role3"]
},
]
- Then it is just about creating java object to represent the payload and do the persistence accordingly
Update
- You can create
ModfifyUsersRoleRequest
like with getters and setters for those 3 fields. Then you can use it as@RequestBody
. Once you are inside the method, do aspring-data-jpa
repository call to get all the users with those user ids and you get a list of users back. Then for each user, add the new roles and remove the roles which you want to remove
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论