This question already has answers here:
JSR-303 @Valid annotation not working for list of child objects
                            
                                (4个答案)
                            
                    
                2年前关闭。
        

    

我有一个请求对象

public class OrderRequest {

    private List<Details> detailsList;

 }

 public class Details{
    Private String id;

    private List<Detail> detailList;

 }


  public class Detail{

    @NotNull(message = "Please provide the inventory name")
    Private String inventoryName;

    Private String inventoryId;

    Private String inventoryLoc;

 }


我想验证每个请求对象的详细信息不为空或不为空。


  javax.validation.constraints.NotNull


为控制器类添加了@valid批注


  @Valid @RequestBody最终OrderRequest
              orderRequest


但它似乎不起作用。我在这里想念什么?

最佳答案

您还应该按以下方式注释OrderRequest(对于Bean Validation 2.0):

public class OrderRequest {
    private List<@Valid Details> detailsList;
}


或者,如果您使用的是较旧的Bean Validation 1.1,则应在列表之前放置`@Valid:

public class OrderRequest {
    private @Valid List<Details> detailsList;
}

09-28 11:39