在List< String>上添加@NotNull或Pattern约束. [英] Adding @NotNull or Pattern constraints on List<String>
本文介绍了在List< String>上添加@NotNull或Pattern约束.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何确保列表中的各个字符串不为null/空白或遵循特定的模式
How can we ensure the individual strings inside a list are not null/blank or follow a specific pattern
@NotNull
List<String> emailIds;
我也想添加图案
@Pattern("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b.")
但是我可以不用它.但是我绝对希望有一个约束来检查列表中的任何字符串是否为空或空白.还有Json模式会是什么样子
but I can live without it.But I would definitely like to have a constraint which will check if any strings inside a list are null or blank. Also how would the Json schema look like
"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
推荐答案
您可以为电子邮件字符串创建一个简单的包装类:
You can create a simple wrapper class for the e-mail String:
public class EmailAddress {
@Pattern("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b.")
String email;
//getters and setters
}
然后在现有对象中标记字段@Valid
:
Then mark the field @Valid
in your existing object:
@NotNull
@Valid
List<EmailAddress> emailIds;
然后,验证器将验证列表中的每个对象.
The validator will then validate each object in the list.
这篇关于在List< String>上添加@NotNull或Pattern约束.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文