Bean Validation 允许使用声明式的方式定义验证规则。
Constrain Once, Validate Anywhere
restriction on a bean, field or property
not null, size between 1 and 7, valid email...
Standard way to validate constraints
Integration with JPA 2.0 & JSF 2.0
举例:
public class Address {
@NotNull @Size(max=30,
message="longer than {max} characters")
private String street1;
...
@NotNull @Valid
private Country country;
}
public class Country {
@NotNull @Size(max=20)
private String name;
...
}
创建自己的验证规则类
@Size(min=5, max=5)
@ConstraintValidator(ZipcodeValidator.class)
@Documented
@Target({ANNOTATION_TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface ZipCode {
String message() default "Wrong zipcode";
String[] groups() default {};
}
没有评论:
发表评论