无法创建配置,因为找不到 Bean 验证提供程序。将 Hibernate Validator (RI)这样的提供程序添加到类路径

今天,我正在寻找发送电子邮件,但当我添加

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>


<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>


<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.5</version>
</dependency>

为了在服务器上部署 pom.xml,我得到了一个“ Unable to create a Configuration,因为找不到 Bean 验证提供程序。将 Hibernate Validator (RI)这样的提供程序添加到类路径中。”验证异常。

我只是添加了上面的依赖关系和一个电子邮件到一个模板 MVC 项目。

错误堆栈跟踪:

DEBUG: org.springframework.ui.context.support.UiApplicationContextUtils - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.DelegatingThemeSource@20212230]
DEBUG: org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean - Failed to set up a Bean Validation provider
javax.validation.ValidationException: 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:271)
at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:223)
at org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean.afterPropertiesSet(OptionalValidatorFactoryBean.java:40)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1241)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4944)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
155118 次浏览

Add a Bean Validation Provider dependency e.g Hibernate Validator. The Bean Validation API dependency is available on the classpath but the implementation is missing. Add the following to your pom.xml

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

You need to add any validation provider dependency for example Hibernate Validator.

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>5.2.1.Final</version>
</dependency>

To resolve the same issue for Spring Boot and Gradle users add following dependency to your project

implementation 'org.springframework.boot:spring-boot-starter-validation'

In my case I didn't need Validators so removing them also removes the error without needing the hibernate-validator dependencies. Regular tests run fine.

public class Test
{


public  ObjectMapper mapper;
public  Validator validator;                //remove
public  ValidatorFactory validatorFactory;  //remove


@Before
public void setUp() {
mapper = new ObjectMapper();
validatorFactory = Validation.buildDefaultValidatorFactory();  //remove
validator = validatorFactory.getValidator();                   //remove
}
...
}

In my case, I moved to Hibernate 7.x but had an old explicit dependency on:

<dependency>
<artifactId>validation-api</artifactId>
<groupId>javax.validation</groupId>
<version>2.0.1.Final</version>
</dependency>

Once I removed this dependency, and allowed Hibernate to transitively pull in the dependency to the Jakarta Bean Validation API (jakarta.validation:jakarta.validation-api:3.0.0). Everything worked fine.

There seems to be an incompatibility with Guice and/or Reactor.io in some of the versions. This worked for me in a Spring Boot WebFlux solution:

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
</dependency>
import static org.assertj.core.api.Assertions.assertThat;


import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Pattern;
import lombok.Data;
import org.junit.jupiter.api.Test;


public class ValidationTests {


@Test
public void validate(){
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Foo foo = new Foo();
foo.setName("7of9");
assertThat(validator.validate(foo).size()).isNotZero();
}


@Data
class Foo{


@Pattern(regexp = "[a-zA-Z]", message = "Only letters allowed")
private String name;


}
}

Otherwise I'd be getting the same error as reported or getting build errors (Guice would become excluded from classpath.

Hibernate Validator requires an implementation of Jakarta Expression Language for evaluating dynamic expressions in constraint violation messages.

When your application runs in a Java EE container such as JBoss Application Server, an EL implementation is already provided by the container.

In a Java SE environment, however, you have to add an implementation as dependency to your POM or gradle file.

For instance you can add the following dependency to use the Jakarta EL reference implementation:

maven

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<version>4.0.2</version>
</dependency>

or gradle

implementation 'org.glassfish:jakarta.el:4.0.2'

Hibernate validator documentation

If you are using spring-boot-starter-validation in your project, hibernate-validator and jakarta.el are already in the dependencies of the given starter.

spring-boot-starter-validation

In my case helped to add the next dependency:

        <dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.1</version>
</dependency>

I made it work downgrading the dependency of hibernate-validator from version 7.0.2.Final to 6.0.13.Final.

This is how my dependencies look like:

    <dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
</dependency>

You can only add one dependency, and resolve the error.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

org.springframework.boot:spring-boot-starter-validation has following dependencies, which are org.glassfish:jakarta.eland org.hibernate.validator:hibernate-validator. Then, org.hibernate.validator:hibernate-validator has jakarta.validation:jakarta.validation-api.

enter image description here

on Jun 2022, it should helps:

pom.xml

<dependencies>
...
<!-- https://mvnrepository.com/artifact/jakarta.el/jakarta.el-api -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator-annotation-processor -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.4.Final</version>
</dependency>
...
</dependencies>

imports in class:

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
...


If you are using spring-boot-starter dependencies, then prefer adding another one for validation.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>