小编典典

在列表上添加@NotNull或Pattern约束

json

我们如何确保列表中的各个字符串不为null /空白或遵循特定的模式

@NotNull
List<String> emailIds;

我也想添加图案

@Pattern("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b.")

但是我可以不用它。但是我绝对希望有一个约束,它可以检查列表中的任何字符串是否为null或空白。而且Json模式看起来如何

"ids": {
      "description": "The  ids associated with this.", 
    "type": "array",
        "minItems": 1,
        "items": {
        "type": "string",
         "required" :true }
 }

"required" :true does not seem to do the job

阅读 583

收藏
2020-07-27

共1个答案

小编典典

您可以为电子邮件字符串创建一个简单的包装类:

public class EmailAddress {

    @Pattern("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b.")
    String email;

    //getters and setters
}

然后@Valid在现有对象中标记该字段:

@NotNull
@Valid
List<EmailAddress> emailIds;

然后,验证器将验证列表中的每个对象。

2020-07-27