我创建了一个自定义验证,检查是否存在电话或手机,但是当验证失败时,弹簧会引发ConstraintViolationException,并且控制器建议不适用于此异常:
@Documented
@Constraint(validatedBy = [CellphoneOrPhoneIsRequiredValidator::class])
@Target(AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS)
@Retention(RetentionPolicy.RUNTIME)
annotation class CellphoneOrPhoneIsRequired(
val message: String = "Cellphone or phone is required",
val groups: Array<KClass<*>> = [],
val payload: Array<KClass<out Payload>> = []
)
class CellphoneOrPhoneIsRequiredValidator: ConstraintValidator<CellphoneOrPhoneIsRequired, Debtor> {
override fun isValid(debtor: Debtor, context: ConstraintValidatorContext): Boolean {
if(debtor.cellphone.isNullOrEmpty() && debtor.phone.isNullOrEmpty()) {
return false
}
return true
}
}
@Entity(name = "debtors")
@CellphoneOrPhoneIsRequired
class Debtor(