Hibernate Validator : Custom Meassages

64 views Asked by At

i have a java project and use a custom hibernate validator in that. According to Hibernate Docs , custom error messages should be defined as key-value in ValidateMessages.properties and this file must be created in "classpath" directory. my problem is that classpath is under "target" directory and it will be deleted after clean-build the project so the created .properties file will be gone. how it can be solved?

`@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
 @Retention(RUNTIME)
 @Constraint(validatedBy = NCValidator.class)
 @Documented
 public @interface NC {

String message() default "{msg}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};}

///////////////////////////////

     ` public class NCValidator implements 
      ConstraintValidator<NC, String> {

     @Override
     public void initialize(NC constraintAnnotation) {
     ConstraintValidator.super.initialize(constraintAnnotation);
      }

    @Override
    public boolean isValid(String string, ConstraintValidatorContext 
    context) {
    ...
    ...
    }

  }`

and use this custom validator in a class like this:

 `@ValidateNC
  default public String getNC() {
     return (String) get("nC");
  }
 `
1

There are 1 answers

4
Sam On

Put your .properties file under your resources folder or anywhere in your system and point to it.