首页   >   代码编程   >   JAVA开发

Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

以前我的个人博客中的入参校验都是用if来做的,效果有,但是不够优雅,这次升级的过程中,我也将它废掉了,直接引入了hibernate-validator校验框架,这么简单的一个问题,竟然也报错了:“Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath”,找到问题原因之后,觉着挺有意思,特写此文记录下来,以便自己以后查阅,也方便他人碰到相同的问题时有可参考的资料。

Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

详细错误如下:

2020-09-17 23:48:22 [ERROR] - com.wolffy.core.common.ServiceExecutor - 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.wolffy.jwcz.util.ValidatorUtils.
valid(ValidatorUtils.java:19)
at com.wolffy.jwcz.service.impl.ArticleServiceImpl.
update(ArticleServiceImpl.java:176)
at com.wolffy.jwcz.service.impl.ArticleServiceImpl.
update(ArticleServiceImpl.java:58)
at com.wolffy.jwcz.service.impl.ArticleServiceImpl$$FastClassBySpringCGLIB$$9dca5f66.
invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.
invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.
invokeJoinpoint(CglibAopProxy.java:721)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.
proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.
invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.
invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.
invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.
proceed(MethodInvocationProceedingJoinPoint.java:85)
at com.wolffy.jwcz.aop.BaseAspect.
doAround(BaseAspect.java:45)
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)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.
invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.
invokeAdviceMethod(AbstractAspectJAdvice.java:618)
at org.springframework.aop.aspectj.AspectJAroundAdvice.
invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.
invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.
intercept(CglibAopProxy.java:656)
at com.wolffy.jwcz.service.impl.ArticleServiceImpl$$EnhancerBySpringCGLIB$$6df15c1d.
update(<generated>)
at com.wolffy.jwcz.controller.ArticleController$1.
doExecute(ArticleController.java:119)
at com.wolffy.jwcz.controller.ArticleController$1.
doExecute(ArticleController.java:109)
at com.wolffy.core.common.ServiceExecutor.
execute(ServiceExecutor.java:84)
at com.wolffy.jwcz.controller.ArticleController.
saveOrUpdate(ArticleController.java:121)
at com.wolffy.jwcz.controller.ArticleController$$FastClassBySpringCGLIB$$71f5b32e.
invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.
invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.
invokeJoinpoint(CglibAopProxy.java:721)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.
invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.
invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.
invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.
proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.
intercept(CglibAopProxy.java:656)
at com.wolffy.jwcz.controller.ArticleController$$EnhancerBySpringCGLIB$$fca169e.
saveOrUpdate(<generated>)
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)
at org.springframework.web.method.support.InvocableHandlerMethod.
doInvoke(InvocableHandlerMethod.java:220)
at org.springframework.web.method.support.InvocableHandlerMethod.
invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.
invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.
invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.
handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.
handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.
doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.
doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.
processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.
doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.
service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.
service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.
service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.
internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.
doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.
doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.
internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.
doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.
doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.
doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.
internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.
doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.
invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.
invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.
invoke(AuthenticatorBase.java:543)
at org.apache.catalina.core.StandardHostValve.
invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.
invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.
invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.
invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.
service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.
service(Http11Processor.java:615)
at org.apache.coyote.AbstractProcessorLight.
process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.
process(AbstractProtocol.java:818)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.
doRun(NioEndpoint.java:1626)
at org.apache.tomcat.util.net.SocketProcessorBase.
run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.
runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.
run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.
run(TaskThread.java:61)
at java.lang.Thread.
run(Thread.java:748)

这个问题出现的原因很简单,只要不是java新手,基本上一眼就可以看出,那就是没有引入hibernate-validator框架包导致的问题。

我为什么说它出现的很有意思呢?

正常来说,如果我没有引入jar包,是使用不了框架提供的注解的,但是我在使用@NotNull这些注解的时候,它却没有报错,这也导致我没有第一时间发现问题的原因所在,直到报错了之后,我又仔细检查了一下路径,发现并没有引入错,确实是javax.validation.constraints,我点进去看了一下,发现在import的时候,使用的是jdk自带的javaee-api包中的@NotNull注解,它的包路径和hibernate-validator的包路径一模一样。

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<!--这里必须是8.0,因为我使用的hibernate-validator是6.0.13.Final版本-->
<version>8.0</version>
</dependency>

注意上面那行注释信息,具体原因请移步:java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String

好了,问题就是出在这里了,虽然jdk提供了一些列的注解,但是具体的校验逻辑,咱还是得依赖hibernate-validator呀!!!

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

完事儿,OJBK了。

雨落无影

QQ群Ⅰ: 686430774 (已满)

QQ群Ⅱ: 718410762 (已满)

QQ群Ⅲ: 638620451 (已满)

QQ群Ⅳ: 474195684 (已满)

QQ群Ⅴ: 463034360 (已满)

QQ群Ⅵ: 879266502 (已满)

QQ群Ⅶ: 627786015 (已满)

工作5分钟,吹逼2小时: 855525339 (娱乐消遣,广告狗勿进)

如果文章有帮到你,可以考虑请博主喝杯咖啡!

分享到:

欢迎分享本文,转载请注明出处!

作者:不忘初心

发布时间:2020-10-01

永久地址:https://www.jiweichengzhu.com/article/a565ab0c727a472cb9d32981bb3d858f

评论