在尝试实现“休眠验证器”时获取NoProviderFoundException

时间:2018-12-03 16:26:23

标签: java gradle hibernate-validator

我得到“无法创建配置,因为找不到Bean验证提供程序。将Hibernate Validator(RI)之类的提供程序添加到类路径中。”尝试为我的实体(ProductInstance)实施验证时出现此错误

这是用法

try {
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        for (ConstraintViolation<ProductInstance> violation : validator.validate(product)) {
            genActResponse.addErrorMessage(violation.getMessage());
            genActResponse.addFailure();
            return genActResponse;
        }
    } catch (Exception e) {
        throw e;
    }

这是gradle.build中的依赖项

dependencies {
compile group: 'org.hibernate', name: 'hibernate-validator-annotation-processor', version: '6.0.2.Final'

这是我遇到的错误

2018-12-03 20:22:12,420 WebErrorHandler                     ERROR - General exception
javax.validation.NoProviderFoundException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:291)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:103)
    at com.helpsystems.incn.access.service.DefaultProductInstanceService.updateProduct(DefaultProductInstanceService.java:200)
    at com.helpsystems.incn.server.ProductInstanceController.updateProduct(ProductInstanceController.java:95)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

1 个答案:

答案 0 :(得分:1)

您需要添加Hibernate验证程序。添加以下之一:

对于Gradle,添加:

compile group: 'org.hibernate', name: 'hibernate-validator', version: '6.0.13.Final'

对于Maven,添加:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.13.Final</version>
</dependency>

您可以根据其他依赖项版本来调整版本