Kotlin Spring验证因类验证而失败

时间:2020-09-18 13:58:33

标签: spring spring-boot kotlin spring-validator

我创建了一个自定义验证,检查是否存在电话或手机,但是当验证失败时,弹簧会引发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(

0 个答案:

没有答案
相关问题