0

我有一個Java項目,並在其中使用自定義的Hibernate驗證器。根據Hibernate Docs,自定義錯誤消息應該在ValidateMessages.properties中定義爲鍵值,並且該文件必須在「classpath」目錄中創建。 我的問題是,類路徑在「目標」目錄下,並且在清理構建項目後它將被刪除,因此創建的.properties文件將消失。如何解決?Hibernate驗證器:自定義的按鈕

`@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) { 
    ... 
    ... 
    } 

    }` 

,並使用該自定義驗證在這樣的類:

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

把它放在'src/main/resources' – Ramanlfc

+0

我把它放在資源下但它沒有工作。我應該如何指向這個目錄? @Ramanlfc – HoseinPanahi

回答

1

把你.properties文件的resources文件夾下或任何在您的系統,並指向它。

+0

i'v添加自定義驗證碼的問題。但我不明白如何指向新的文件地址。請解釋 – HoseinPanahi

+0

你能分享你的項目結構嗎? –

+0

你是什麼意思關於結構?我定義了一個自定義驗證器和一個註釋,並在類中使用該註釋來獲取bean的屬性。然後,我在目標/類中創建了「ValidateMessages.properties」,並在其中定義了鍵值。它的工作原理,但清理後的目標將消失。 @ Samuel.F – HoseinPanahi