5
我在我的項目中使用bean驗證,我想爲現有約束註釋編寫自定義驗證程序。爲現有約束註冊自定義ConstraintValidator
例如,我有一個代表日期/時間名爲CustomDateTime
的類。在使用這個類,例如我想註釋字段@Past
出生日期一類:
public class Person
{
@Past
private CustomDateTime dateOfBirth;
}
我然後創建通過實施ConstraintValidator<Past, CustomDateTime>
一個自定義的驗證。 但是這不起作用,因爲驗證實現沒有定製驗證器的知識。然後它拋出:javax.validation.UnexpectedTypeException: No validator could be found for type: com.example.CustomDateTime
。
我知道,你通常會創建一個單獨的註釋是這樣的:
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {CustomDateTimePastValidator.class})
public @interface Past
{
....
}
但是,這似乎是雙碼我;-)
如何註冊自定義驗證與@Past
使用?
@ siebz0r它對你有用嗎? –