Springboot request object validation [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-11 17:51:00

问题


I have a request object

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;

 }

and i want to validate each request object Detail for not null or not empty.

javax.validation.constraints.NotNull

@valid annotation is added for the controller class

@Valid @RequestBody final OrderRequest orderRequest

but it doesn't seem to work. what am i missing here?


回答1:


You should also annotate your OrderRequest as follows (in case of Bean Validation 2.0):

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

Or if you are using an older Bean Validation 1.1 you should place the `@Valid before the list:

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


来源:https://stackoverflow.com/questions/51040451/springboot-request-object-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!