定义扩展现有验证注解的自定义验证注解 - javaEE
Define custom validation annotation that extends an existing one - javaEE
我想定义一个使用 @Pattern
注释的自定义 @DateOfBirthValid
验证注释,以便我定义一次出生日期模式并在任何地方使用它...我知道我可以定义它使用 @Constraint(validatedBy = SomeClass.class)
然后在 SomeClass
中定义 isValid()
方法但我想知道是否有更直接的方法来使用 @Pattern
注释并给它出生日期正则表达式我想要...原因是我想在代码中的很多地方使用相同的验证注解而不需要再次定义模式
来自https://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html:
Bean Validation includes several built-in constraints that can be combined to create new, reusable constraints. This can simplify constraint definition by allowing developers to define a custom constraint made up of several built-in constraints that may then be applied to component attributes with a single annotation.
在 link 中的示例中,您可以看到如何使用 @Pattern
(以及其他)创建电子邮件验证注释:
@Pattern.List({
@Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\."
+"[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*"
+"@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
})
@Constraint(validatedBy = {})
@Documented
@Target({ElementType.METHOD,
ElementType.FIELD,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Email {
String message() default "{invalid.email}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({ElementType.METHOD,
ElementType.FIELD,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface List {
Email[] value();
}
}
因此,根据我(相当有限)对此的理解,您应该能够创建一个与此相似的新注释
@Pattern(regexp = yourRegex)
@Documented
@Target(/* all targets here */)
@Retention(RetentionPolicy.RUNTIME)
public @interface DateOfBirthValid {
/* all methods required */
/* Same thing for the List interface */
}
然后应该可以像
一样使用
@DateOfBirthValid
protected String myString
或来自 link
的示例
This custom constraint can then be applied to an attribute.
...
@Email
protected String email;
...
我想定义一个使用 @Pattern
注释的自定义 @DateOfBirthValid
验证注释,以便我定义一次出生日期模式并在任何地方使用它...我知道我可以定义它使用 @Constraint(validatedBy = SomeClass.class)
然后在 SomeClass
中定义 isValid()
方法但我想知道是否有更直接的方法来使用 @Pattern
注释并给它出生日期正则表达式我想要...原因是我想在代码中的很多地方使用相同的验证注解而不需要再次定义模式
来自https://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html:
Bean Validation includes several built-in constraints that can be combined to create new, reusable constraints. This can simplify constraint definition by allowing developers to define a custom constraint made up of several built-in constraints that may then be applied to component attributes with a single annotation.
在 link 中的示例中,您可以看到如何使用 @Pattern
(以及其他)创建电子邮件验证注释:
@Pattern.List({
@Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\."
+"[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*"
+"@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
})
@Constraint(validatedBy = {})
@Documented
@Target({ElementType.METHOD,
ElementType.FIELD,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Email {
String message() default "{invalid.email}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({ElementType.METHOD,
ElementType.FIELD,
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface List {
Email[] value();
}
}
因此,根据我(相当有限)对此的理解,您应该能够创建一个与此相似的新注释
@Pattern(regexp = yourRegex)
@Documented
@Target(/* all targets here */)
@Retention(RetentionPolicy.RUNTIME)
public @interface DateOfBirthValid {
/* all methods required */
/* Same thing for the List interface */
}
然后应该可以像
一样使用@DateOfBirthValid
protected String myString
或来自 link
的示例This custom constraint can then be applied to an attribute.
...
@Email
protected String email;
...