Java 由于 Bean Validation API,无法启动 Hibernate Validator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21656146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:53:59  来源:igfitidea点击:

Trouble starting Hibernate Validator due to Bean Validation API

javahibernatemavenhibernate-validator

提问by Click Upvote

I'm trying to use Hibernate Validator in my project, but it isn't working. On the following line:

我正在尝试在我的项目中使用 Hibernate Validator,但它不起作用。在以下行:

SessionFactory sessions = config.buildSessionFactory(builder.build());

I get the following exception:

我收到以下异常:

org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
    at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:154)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:311)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
    at net.myProject.server.util.HibernateUtil.<clinit>(HibernateUtil.java:32)
    ... 36 more
Caused by: java.lang.NoSuchMethodError: javax.validation.spi.ConfigurationState.getParameterNameProvider()Ljavax/validation/ParameterNameProvider;
    at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.<init>(ValidatorFactoryImpl.java:119)
    at org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:45)
    at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:217)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)

I found this questionwhich seems quite similar to my problem. He describes his solution as

我发现这个问题似乎与我的问题非常相似。他将他的解决方案描述为

I had yet another bean validator jar in the class path. But not from maven, so i didn't realize it. Removing that solved the problem.

我在类路径中还有另一个 bean 验证器 jar。但不是来自maven,所以我没有意识到。删除它解决了问题。

I think my problem is the same. On http://hibernate.org/validator/documentation/getting-started/it says:

我想我的问题是一样的。在http://hibernate.org/validator/documentation/getting-started/ 上,它说:

This transitively pulls in the dependency to the Bean Validation API (javax.validation:validation-api:1.1.0.Final)

这会传递依赖 Bean Validation API (javax.validation:validation-api:1.1.0.Final)

That must be causing this issue, since reverting to an older version (4.3.1.Final) fixes the issue. Is there a way to force Hibernate to not pull in the Bean Validation API?

这一定是导致此问题的原因,因为恢复到旧版本 (4.3.1.Final) 可以解决该问题。有没有办法强制 Hibernate 不引入 Bean Validation API?

Edit: I've tried to exclude the javax-validation api:

编辑:我试图排除 javax-validation api:

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.0.3.Final</version>
      <exclusions>
          <exclusion>
              <groupId>javax.validation</groupId>
              <artifactId>validation-api</artifactId>
          </exclusion>
      </exclusions>
  </dependency>

But it didn't seem to have any effect.

但它似乎没有任何效果。

采纳答案by Koitoer

Try adding this dependency to your pom.xml

尝试将此依赖项添加到您的 pom.xml

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
</dependency>

If not consider using hibernate-validator4.2.0.Final I have that one in my config and it is working fine.

如果不考虑使用 hibernate-validator4.2.0.Final 我的配置中有那个并且它工作正常。

回答by Shweta Tiwari

Removing this jar javax.validation:validation-api:1.1.0.Finalsolved my problem.

删除这个 jarjavax.validation:validation-api:1.1.0.Final解决了我的问题。

Make sure you have only one validation jar. If we have two jars then they may conflict resulting in error.

确保您只有一个验证 jar。如果我们有两个 jars,那么它们可能会发生冲突导致错误。

回答by Pijush

For me, the 1.1.0.Final version javax.validation.validation-api had worked. Because, the javax.validation.spi.ConfigurationState interface of 1.1.0.Final has getParameterNameProvider method, which was absent in 1.0.0.GA.

对我来说,1.1.0.Final 版本 javax.validation.validation-api 已经工作了。因为,1.1.0.Final 的 javax.validation.spi.ConfigurationState 接口有 getParameterNameProvider 方法,1.0.0.GA 中没有。

I added the below dependency in pom.xml

我在 pom.xml 中添加了以下依赖项

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
           <scope>test</scope>
</dependency>

回答by siddharth chaudhary

in my case i just deleted the hibernate-validator and it worked .(i also had a combo of both validation api and hibernate-validator and tried everything) or you can go to your maven repository-->org and then delete the hibernate folder and rebuild your project again.. hope it helps..

在我的情况下,我刚刚删除了 hibernate-validator 并且它起作用了。(我也有验证 api 和 hibernate-validator 的组合并尝试了所有方法)或者您可以转到您的 maven 存储库--> org,然后删除 hibernate 文件夹并再次重建您的项目..希望它有帮助..

回答by Ian Boyd

I thought it would be useful to explain what is going on here.

我认为解释这里发生的事情会很有用。

Hibernate is calling ConfigurationState.getParameterNameProvider:

Hibernate 正在调用ConfigurationState.getParameterNameProvider

ValidatorFactoryImpl.java:

ValidatorFactoryImpl.java:

public ValidatorFactoryImpl(ConfigurationState configurationState) {
   ...
   configurationState.getParameterNameProvider()
   ...
}

You can find the documentation of getParameterNameProvider:

您可以找到以下文档getParameterNameProvider

getParameterNameProvider

ParameterNameProvidergetParameterNameProvider()

Returns the parameter name provider for this configuration.

Returns:

parameter name provider instance or null if not defined

Since:

1.1

获取参数名称提供程序

ParameterNameProvidergetParameterNameProvider()

返回此配置的参数名称提供程序。

返回:

参数名称提供程序实例或 null 如果未定义

自从:

1.1

So what's the problem? The problem is that the method didn't always exist. It was added at some point in the future.

所以有什么问题?问题是这种方法并不总是存在。它是在将来的某个时候添加的。

And the rule when creating interfaces is that they are set in concrete: you shall not change an interface ever. Instead the JavaX validator changed the ConfigurationStateinterface, and added a few new methods over the years.

和创建接口时的规则是,他们在具体设置:您不得更改接口不断。相反,JavaX 验证器更改了ConfigurationState接口,并且多年来添加了一些新方法。

The java validation code is passing the Hiberate an outdated ConfiguationStateinterface; one that doesn't implement the required interfaces.

java 验证代码正在向Hiberate传递一个过时的ConfiguationState接口;一个没有实现所需接口的。

You need to ensure that javax.validation.Validation.buildDefaultValidatorFactory is updated to to support version 1.1.

您需要确保将 javax.validation.Validation.buildDefaultValidatorFactory 更新为支持 1.1 版。

回答by Paulo

Go to the dependecies project and delete, hibernate.validator, and reinstall that in the most recent version. It has solved the problem for me.

转到dependecies 项目并删除hibernate.validator,然后在最新版本中重新安装它。它为我解决了这个问题。

回答by Paulo

I had the problem again. Thats how I've fixed that:

我又遇到了这个问题。我就是这样解决的:

1-Exclude spring.validator from the 'web' dependency:

1-从“web”依赖项中排除 spring.validator:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

2-After insert the dependecy with a previous version:

2-插入以前版本的依赖项后:

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