2012-10-01 58 views
3

我使用的是Spring 3.0。我沒有使用hibernate或maven依賴項。我已經下載了這些jar文件並將其添加到我的類路徑中。我收到一個javax.validation.ValidationException。在研究了這個問題之後,我將hibernate-core-3.5.6.jar和slf4-api.1.4.3.jar文件包含到classpath中。SpringMvc - javax.validation.ValidationException

錯誤日誌

2073 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is javax.validation.ValidationException: Unable to find a default provider 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) 
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) 
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) 
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) 
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) 
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) 
    at javax.servlet.GenericServlet.init(GenericServlet.java:212) 
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206) 
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026) 
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734) 
    at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1284) 
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1382) 
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:306) 
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142) 
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1389) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: javax.validation.ValidationException: Unable to find a default provider 
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:264) 
    at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:184) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) 
    ... 29 more 

回答

9

您需要包括hibernate-validator library jar

Hibernate Validator(版本4.x及以上版本)是javax.validation.constraints的參考實現,也稱爲JSR-303註釋。
儘管有這個名稱,但這個庫與數據庫和關係映射無關,它只是進行驗證。

+0

我沒有包括休眠,validator.jar但我仍然得到以下錯誤:org.springframework.beans.factory .BeanCreationException:創建名爲'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0'的Bean時出錯:init方法的調用失敗;嵌套異常是javax.validation.ValidationException:無法找到默認提供程序。我是否需要在applicationContext.xml中指定任何bean? – devdar

+0

@dev_darin您使用的是hibernate-validator的版本嗎? –

+0

我現在正在檢查,我看到3.0我需要使用4.0和更早? – devdar

0

我有同樣的問題...我有這個,卜不想使用驗證:

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

添加具有實現JSR 303(Bean驗證)的休眠驗證器的依賴。

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

我加了休眠,驗證在pom.xml的依賴和它的工作