将对象列表发送到请求正文时,Spring Boot 中的验证不起作用

问题描述 投票:0回答:1

我正在发送一个对象列表来请求正文,如下所示:

@PatchMapping("/branches")
    public MicApiResponse<Void> updateForManyBranches(
            @RequestBody List<@Valid RepresentativeForManyBranchesRequest> req) throws MicClientException {
        service.updateForManyBranches(req);
        return MicApiResponse.succeed();
    }
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RepresentativeForManyBranchesRequest {
    @Valid
    private RepresentativeUserForManyBranchesRequest representativeUsers;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RepresentativeUserForManyBranchesRequest {
    private String id;
    @Required
    private String userId;
    @Required
    @Length(min = 1, max = 100)
    private String name;
    @Required
    @Length(min = 1, max = 100)
    private String position;}

当我仅发送一个对象到请求正文时,这些注释 @Required 和 @Length 起作用,但是当我发送对象列表时,它们不起作用。我应该怎么做才能解决这个问题?非常感谢

java spring-boot
1个回答
0
投票

我觉得你应该这样写

public MicApiResponse<Void> updateForManyBranches(@RequestBody @Valid List<RepresentativeForManyBranchesRequest> req)
© www.soinside.com 2019 - 2024. All rights reserved.