追风的茶壶 · CheckStateChanged(复选框选 ...· 3 月前 · |
还单身的脆皮肠 · 不能从'skimage.feature'导入 ...· 1 年前 · |
不敢表白的台灯 · Sysinternals 进程实用工具 - ...· 1 年前 · |
精明的铁链 · C++ cv::Mat转unity ...· 1 年前 · |
今天,我正在查看发送电子邮件,但当我添加
<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和部署时,我得到一个“无法创建配置,因为找不到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)
添加Bean验证提供程序依赖项,例如Hibernate Validator。Bean验证API依赖项在类路径上可用,但缺少实现。将以下内容添加到您的pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
您需要添加任何验证提供程序依赖项,例如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>
要解决Spring Boot中的相同问题,请将以下依赖项添加到项目中
implementation 'org.springframework.boot:spring-boot-starter-validation'
在我的例子中,我不需要验证器,因此删除它们也可以消除错误,而不需要hibernate验证器依赖项。常规测试运行良好。
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
}
在我的例子中,我转移到了Hibernate 7.x,但对以下内容有一个旧的显式依赖:
<dependency>
<artifactId>validation-api</artifactId>
<groupId>javax.validation</groupId>
<version>2.0.1.Final</version>
</dependency>
一旦我移除了这个依赖项,并允许Hibernate将这个依赖项传递到Jakarta Bean验证API (jakarta.validation:jakarta.validation-api:3.0.0).中一切都很好。
在某些版本中,似乎与Guice和/或Reactor.io不兼容。在Spring Boot WebFlux解决方案中,这对我很有效:
<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;
}
否则,我将得到与报告相同的错误或获得构建错误(Guice将被排除在类路径之外。
在我的例子中,帮助添加了下一个依赖项:
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.1</version>
</dependency>
我把hibernate验证器的依赖从
7.0.2.Final
版本降级到了
6.0.13.Final
。
下面是我的依赖项的样子:
<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>