如何根据另一个注释定义一个 Java 注释?
How can I define one Java annotation in terms of another?
例如,我希望 @Nonnegative
定义为 @Min(0)
,@DaySeconds
定义为 @Min(0) @Max(86399)
。
两者都@Min
and @Max
annotations can be used on annotations themselves. This is called constraint composition。
因此,您可以像这样定义一个新约束 DaySeconds
:
@Min(0)
@Max(86399)
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface DaySeconds {
String message() default "{your.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@Nonnegative
也是一样。
例如,我希望 @Nonnegative
定义为 @Min(0)
,@DaySeconds
定义为 @Min(0) @Max(86399)
。
两者都@Min
and @Max
annotations can be used on annotations themselves. This is called constraint composition。
因此,您可以像这样定义一个新约束 DaySeconds
:
@Min(0)
@Max(86399)
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface DaySeconds {
String message() default "{your.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
@Nonnegative
也是一样。