Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.validation.ConstraintDefinitionException: HV000074 in Spring MVC

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

like image 274
Anirudh Jadhav Avatar asked Dec 19 '22 02:12

Anirudh Jadhav


1 Answers

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 {}; 
like image 74
Grzegorz Gralak Avatar answered May 11 '23 03:05

Grzegorz Gralak