本文介绍了Springboot请求对象验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个请求对象

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;

 }

,并且我想验证每个请求对象的详细信息是否不为null或不为空.

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

为控制器类添加

@valid注释

@valid annotation is added for the controller class

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

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

推荐答案

您还应该按如下方式注释OrderRequest(在Bean Validation 2.0的情况下):

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

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

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

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;
}

这篇关于Springboot请求对象验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 18:53