如何使用端点将一个事物的列表列表分配给另一个事物的列表。

huangapple go评论56阅读模式
英文:

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 = &quot;/roles/{id}&quot;, consumes = MediaType.APPLICATION_JSON_VALUE)
public UserDto modifyRoles(
@ApiParam(value = &quot;User ID&quot;, required = true) @PathVariable(&quot;id&quot;) Long id,
@ApiParam(value = &quot;List of ID roles&quot;, required = true) @RequestBody List&lt;Long&gt; 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 = &quot;/roles/{usersId}&quot;, consumes = MediaType.APPLICATION_JSON_VALUE)
public List&lt;UserDto&gt; modifyMultipleRoles(
@ApiParam(value = &quot;List of User ID&quot;, required = true) @PathVariable(&quot;id&quot;) List&lt;Long&gt; ids,
@ApiParam(value = &quot;List of ID roles&quot;, required = true) @RequestBody List&lt;Long&gt; 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&lt;Long&gt; usersId=objectDto.getUsersId();
List&lt;Long&gt; rolesToAdd = objectDto.getRolesToAdd();
List&lt;Long&gt; rolesToDelete = objectDto.getRolesToRemove();
List&lt;Usuario&gt; users = usersId.stream()
.map(userId -&gt; 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

{
&quot;users&quot;: [&quot;user1&quot;, &quot;user2&quot;],
&quot;roles&quot;: [&quot;role1&quot;, &quot;role2&quot;]
}
public class RolesRequestBoday {
public List&lt;String&gt; users;
public List&lt;String&gt; 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&lt;Long&gt; users;
private List&lt;Long&gt; rols;
}

The final result shoul be like this:

@PutMapping(value = &quot;/roles/{usersId}&quot;, consumes = MediaType.APPLICATION_JSON_VALUE)
public List&lt;UserDto&gt; modifyMultipleRoles(@ApiParam(value = &quot;Object with the list of ID 
users and the List of ID roles&quot;, required = true) @RequestBody UsersRolsRequest 
request)throws someExceptions {
return serviceClass.newMethodThatAssignEachRoleToEachUser(ids, roles);
}

The request json will be like:

{
&quot;users&quot;: [1,2,3],
&quot;rols&quot;: [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
{
   "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 users and roles

  • 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

     {
&quot;users&quot;: [&quot;user1&quot;, &quot;user2&quot;],
&quot;roles&quot;: [&quot;role1&quot;, &quot;role2&quot;]
}

or

    [
{ 
&quot;id: &quot;user1&quot;
&quot;roles&quot;: [&quot;role1&quot;, &quot;role2&quot;]
},
{ 
&quot;id: &quot;user2&quot;
&quot;roles&quot;: [&quot;role2&quot;, &quot;role3&quot;]
},
]
  • 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
      {
&quot;users&quot;: [&quot;user1&quot;, &quot;user2&quot;],
&quot;rolesToAdd&quot;: [&quot;role1&quot;, &quot;role2&quot;]&quot;,
&quot;rolesToRemove&quot;: [&quot;role3&quot;]
}
    [
{ 
&quot;id: &quot;user1&quot;
&quot;rolesToAdd&quot;: [&quot;role1&quot;],
&quot;rolesToRemove&quot;: [&quot;role2&quot;],
},
{ 
&quot;id: &quot;user2&quot;
&quot;rolesToAdd&quot;: [&quot;role2&quot;, &quot;role3&quot;],
&quot;rolesToRemove&quot;: [&quot;role4&quot;],
},
]
      {
&quot;users&quot;: [&quot;user1&quot;, &quot;user2&quot;],
&quot;roles&quot;: [&quot;role1&quot;, &quot;role2&quot;]&quot;,
}
    [
{ 
&quot;id: &quot;user1&quot;
&quot;roles&quot;: [&quot;role1&quot;, &quot;role2&quot;]
},
{ 
&quot;id: &quot;user2&quot;
&quot;roles&quot;: [&quot;role2&quot;, &quot;role3&quot;]
},
]
  • 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 a spring-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

huangapple
  • 本文由 发表于 2020年8月2日 01:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63208234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定