参数校验 工具包validation-api

 Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator()
ABean aben= new Abean();
VALIDATOR .validator(aben, GroupAdd.class) //GroupAdd为注解里

class ABean{
@NotNull(groups = { GroupUpdate.class, GroupAdd.class }, message = "不能为空")
@Size(groups = { GroupUpdate.class, GroupAdd.class }, min = 1, max = 64, message = "必须小于64位") //长度校验
private String name;
@Pattern(groups = { GroupUpdate.class, GroupAdd.class }, regexp = "^1[0-9]{10}|[0-9]{8}$", message = "手机格式不正确")
private String phone
}
 
对于嵌套验证 结合@Valid 可完成
 
 
posted @ 2022-01-14 16:37  清明雨下  阅读(321)  评论(0)    收藏  举报