我有这堂课:
public class ClientDomain {
@ValidId
private Long id;
@Size(min = 2, max = 10, message = "Client's name length should be between 2 and 10")
private String name;
// Getters and setters
}
使用
的@ValidId约束@Digits(fraction = 0, integer = 5, message = "Size id invalid")
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface ValidId {
String message() default "Invalid ID";
}
然后我执行这一行
Set<ConstraintViolation<ClientDomain>> violations = this.validator.validate(client);
当我传递无效ID时,不会返回任何违规行为。但是,当我在clientDomain中使用@Digits而不是@ValidId时,如下所示:
public class ClientDomain {
@Digits(fraction = 0, integer = 5, message = "invalid id")
private Long id;
@Size(min = 2, max = 10, message = "Client's name length should be between 2 and 10")
private String name;
// Getters and setters
}
我再次使用无效ID进行验证,这是有效的。如果我没有进行任何附加验证,我不知道为什么不认识我@ValidId。你们对发生了什么有什么想法吗?谢谢!