Issue
I am looking to implement a JPA Bean Validator for a Annotation similiar to the @Size
Annotation, so allowing parameters min
and max
on the annotation. I have been looking through the documentation and various examples but couldn't find an implementation that would allow multiple parameters on the Annotation.
Currently my code looks like this
@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = CapacityImpl.class)
public @interface Capacity {
String message() default "Capacity not in valid Range";
// Required by validation runtime
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
The final Annotation should look like this @Capacity(min=19, max=40)
public class CapacityImpl implements ConstraintValidator<Capacity, Integer> {
@Override
public void initialize(Capacity capacity) {
}
@Override
public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
return false;
}
}
So my question is how do I have to change this interface and class to actually allow the parameters min and max on the Annotation, so that I then can validate the value of the field with those paramter values
Solution
Just add them to your annotation definition:
public @interface Capacity {
String message() default "Capacity not in valid Range";
// Required by validation runtime
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
int min();
int max();
}
Answered By - john16384
Answer Checked By - Katrina (JavaFixing Volunteer)