I have made a simple demo project for my students but I am not able to recognize this error following are the classes please let me know what I am missing.
Interface
package ani.validator;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
@Constraint(validatedBy={CourseCodeContstraintValidator.class})
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CourseCode {
public String value() default "LUV";
public String message() default "Not a proper code";
}
Custom Validation Class
package ani.validator;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CourseCodeContstraintValidator implements ConstraintValidator<CourseCode, String> {
private String prefixCourseCode;
public void initialize(CourseCode theCourseCode){
prefixCourseCode = theCourseCode.value();
}
public boolean isValid(String value, ConstraintValidatorContext arg1) {
if(prefixCourseCode != null){
return value.startsWith(prefixCourseCode);
}
return false;
}
}
Your suggestions, comments are welcome. Thanks in advance
I got that error as well:
javax.validation.constraintdefinitionexception: hv000074
It's about groups()
and payload()
missing in your constraint annotation, just add those 2 lines and you should be good:
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With