For example, I'd like to have @Nonnegative
, defined as @Min(0)
, and @DaySeconds
, defined as @Min(0) @Max(86399)
.
Asked
Active
Viewed 291 times
7

Neil Stockton
- 11,383
- 3
- 34
- 29

Reuben Thomas
- 647
- 6
- 13
-
do you mean nested annotations? – Ramanlfc Nov 23 '15 at 10:13
1 Answers
5
Both @Min
and @Max
annotations can be used on annotations themselves. This is called constraint composition.
As such, you can define a new constraint DaySeconds
like this:
@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 {};
}
It would be the same for @Nonnegative
.

Tunaki
- 132,869
- 46
- 340
- 423