您现在的位置是:网站首页 > 代码编程 > JAVA开发JAVA开发

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

不忘初心 不忘初心 2020-10-01 围观() 评论() 点赞() JAVA开发

简介:以前我的个人博客中的入参校验都是用if来做的,效果有,但是不够优雅,这次升级的过程中,我也将它废掉了,直接引入了validator校验框架,这么简单的一个问题,竟然也报错了:“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了。

springspringmvcvalidator

看完文章,有任何疑问,请加入群聊一起交流!!!

很赞哦! ()

文章评论

  • 请先说点什么
    人参与,条评论

请使用电脑浏览器访问本页面,使用手机浏览器访问本页面会导致下载文件异常!!!

雨落无影

关注上方公众号,回复关键字【下载】获取下载码

用完即删,每次下载需重新获取下载码

若出现下载不了的情况,请及时联系站长进行解决

站点信息

  • 网站程序:spring + freemarker
  • 主题模板:《今夕何夕》
  • 文章统计:篇文章
  • 标签管理标签云
  • 微信公众号:扫描二维码,关注我们