Javax验证

时间:2010-11-29 09:26:46

标签: spring validation

我正在尝试使用Spring进行Javax验证。当我运行我的应用程序时,我收到这个奇怪的错误消息

javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:619)

root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.engine.ConfigurationImpl
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:619)

root cause

java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.engine.ConfigurationImpl
    org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:43)
    javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:269)
    org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:161)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:619)

我的classpath中有hibernate-validator-4.0.2 GA.jar,似乎缺少类。我也用jdk 6.出了什么问题?我用谷歌搜索了,但没有找到答案。

9 个答案:

答案 0 :(得分:13)

堆栈跟踪显示Hibernate Validator和javax.validation API JAR都在你的类路径上,所以问题出在其他地方。

ConfigurationImpl的源代码表明它依赖于SLF4J。

我猜这不是你的类路径?

答案 1 :(得分:9)

我解决了它在Maven项目中将以下内容添加到POM中:

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

您还可以查看official reference

答案 2 :(得分:3)

Jar- hibernate-validator4.3.1 final.jar file- HibernetValidator.java 第41行 -

 public Configuration<?> createGenericConfiguration(BootstrapState state) 
 {
    return new ConfigurationImpl(state);
 }

ConfigurationImpl类有一个静态初始化块

static
 {
     Version.touch();
     log = LoggerFactory.make();
  }

LogFactory类重新生成org.jboss.logging.Logger。

所以将jboss-logging-3.2.0.Final.jar添加到依赖项中,就完成了。

答案 3 :(得分:2)

检查hibernate-validator-4.0.2 GA.jar是否部署在服务器上Web应用程序的WEB-INF / lib目录中。

答案 4 :(得分:2)

找到解决方案。我的slf4j jar版本没有匹配,我也有间接库。

答案 5 :(得分:1)

我的类路径中有slf4j-api的两个版本 - slf4j-api-1.6.1.jar以及slf4j-api-1.5.8.jar。我删除后者,用

解决了我的问题
"java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.engine.ConfigurationImpl".

答案 6 :(得分:1)

对我而言,这是一个失踪的“jboss-logging”jar

答案 7 :(得分:1)

我有同样的问题。通过添加以下依赖关系解决它,尽管不使用hibernate。

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.1.0.Beta1</version>
        </dependency>

答案 8 :(得分:1)

您可能会遇到hibernate-validatorvalidation-api的冲突版本。您可以在此问题中阅读更多相关信息:

JPA 2.0 : Exception to use javax.validation.* package in JPA 2.0

如果您想使用最新的hibernate-validator版本,则应删除validation-api依赖项,并为hibernate-validator添加以下依赖项:

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