0

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");
  }
 `
HoseinPanahi
  • 582
  • 2
  • 8
  • 18

1 Answers1

1

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

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Sam
  • 11
  • 3
  • i'v added custom validator code to the question.But i do not understand how to point to the new file address. please explain – HoseinPanahi Sep 25 '17 at 15:08
  • Can you share your project structure? – Sam Sep 25 '17 at 17:15
  • What do you mean about structure? I defined a custom validator and an annotation and used that annotation in class for a bean proprety. Then I created "ValidateMessages.properties" in target/classes and defiened key-values in it. and it works, but after clean-build target will be gone. @Samuel.F – HoseinPanahi Sep 26 '17 at 13:04
  • Don't create the properties file on `target/classes`, put the file under `src/main/resources` folder. It will be scanned automatically and get your values. or if u prefere to put the file some where else, you can do so, and make sure you point to that file location when you load resources. – Sam Sep 27 '17 at 15:47