如何为"javax.validation.constraints"注释添加自定义验证器? [英] how add custom validator for `javax.validation.constraints` annotation?

查看:238
本文介绍了如何为"javax.validation.constraints"注释添加自定义验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了对 Long 字段的验证和 @PastOrPresent 验证

I have implemented validation for Long field and @PastOrPresent validation

public class PastOrPresentValidator implements ConstraintValidator<PastOrPresent, Long>{
    @Override
    public boolean isValid(Long value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }
        return value <= System.currentTimeMillis();
    }
}

public class MyDto {
    @PastOrPresent
    Long timestamp;    
}

出现错误:

HV000030: No validator could be found for constraint 'javax.validation.constraints.PastOrPresent' validating type 'java.lang.Long'. Check configuration for 'push.dto.timestamp'

但是当我创建自定义注释(如 @PastOrPresentLong )并使用相同的验证器代码时,一切正常.

But when I create custom annotation like @PastOrPresentLong and use same validator code everything works.

是否可以为 javax.validation.constraints 注释添加自定义验证器?

Is there a way to add custom validator for javax.validation.constraints annotation?

推荐答案

仅实现 ConstraintValidator 接口不足以使新的验证器正常工作.应该以某种方式注册它,以便BeanValidation提供程序知道它.这可以通过XML完成(有关更多信息,请参见

Just implementing the ConstraintValidator interface isn't enough for your new validator to work. It should be somehow registered so that BeanValidation provider knows about it. This can be done via XML (see for more info here):

<constraint-mappings
        xmlns="http://xmlns.jcp.org/xml/ns/validation/mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/mapping
            http://xmlns.jcp.org/xml/ns/validation/mapping/validation-mapping-2.0.xsd"
        version="2.0">
    <!-- anything else -->
    <constraint-definition annotation="javax.validation.constraints.PastOrPresent">
        <validated-by include-existing-validators="true">
            <value>org.mycompany.PastOrPresentLongValidator</value>
        </validated-by>
    </constraint-definition>
</constraint-mappings>

然后,如果您将Hibernate Validator用作BeanValidation提供程序,则还有两个选择-使用服务加载程序或编程定义.使用服务加载程序是添加新验证程序的最简单方法.您需要做的只是创建一个 META-INF/services/javax.validation.ConstraintValidator 文件,然后向其添加验证器的FQCN.请参阅此在非标准类使用标准约束"部分或编程方法您将必须执行以下操作:

Then if you are using Hibernate Validator as BeanValidation provider you also have two more options - use service loader or programmatic definition. Using Service loader is the easiest way of adding new validator. All you need to do is create a META-INF/services/javax.validation.ConstraintValidator file and add a FQCN of your validator to it. See detailed examples in this post under section "Use standard constraints for non standard classes" or in the doc. With programmatic approach you would have to do something like:

ConstraintMapping constraintMapping = configuration.createConstraintMapping();

constraintMapping
        .constraintDefinition( PastOrPresent.class )
        .validatedBy( PastOrPresentLongValidator.class );

然后将此约束映射添加到您的配置中,并从中创建一个验证器:

And then add this constraint mapping to your configuration and create a validator from it:

Validator validator = configuration.addMapping( constraintMapping )
        .buildValidatorFactory()
        .getValidator();

但是正如我之前提到的-Service Loader是最简单,最快的方法.

But as I mentioned earlier - Service Loader is the easiest and quickest way to go.

这篇关于如何为"javax.validation.constraints"注释添加自定义验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆