2014-06-24 68 views
61

我試着寫與Hibernate驗證非常簡單的應用:javax.validation.ValidationException:HV000183:無法加載 'javax.el.E​​xpressionFactory'

我的步驟:在pom.xml中

添加以下依賴:

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

寫代碼:

class Configuration { 
    Range(min=1,max=100) 
    int threadNumber; 
    //... 

    public static void main(String[] args) { 
     ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); 

     Validator validator = factory.getValidator(); 

     Configuration configuration = new Configuration(); 
     configuration.threadNumber = 12; 
      //... 

     Set<ConstraintViolation<Configuration>> constraintViolations = validator.validate(configuration); 
     System.out.println(constraintViolations); 

    } 
} 

而且我得到以下stacktra ce:

Exception in thread "main" javax.validation.ValidationException: Unable to instantiate Configuration. 
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:279) 
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110) 
    ... 
    at org.hibernate.validator.internal.engine.ConfigurationImpl.<init>(ConfigurationImpl.java:110) 
    at org.hibernate.validator.internal.engine.ConfigurationImpl.<init>(ConfigurationImpl.java:86) 
    at org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:41) 
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:276) 
    ... 2 more 

我該怎麼做?

+0

升級的Hibernate驗證器'5.2.4.Final'解決的問題給我。 – fracz

+0

@fracz我有hibernate-validator ='5.2.4.Final',異常仍然存在。 –

回答

113

它也加入到pom.xml以下相關工作後:

<dependency> 
    <groupId>javax.el</groupId> 
    <artifactId>javax.el-api</artifactId> 
    <version>2.2.4</version> 
</dependency> 
<dependency> 
    <groupId>org.glassfish.web</groupId> 
    <artifactId>javax.el</artifactId> 
    <version>2.2.4</version> 
</dependency> 

Getting started with Hibernate Validator

+4

Bean Validation 1.1需要類路徑上的表達式語言依賴關係。另請參見http://hibernate.org/validator/documentation/getting-started/ – Hardy

+0

\t org.glassfish.web \t javax.el \t 2.2.4 \t 運行 就足夠了,因爲hibernate驗證器已經取決於javax.el-api – mvera

+7

' javax.el javax.el-API 2.3.1'就足夠了,我 – ArtB

48

做到這

<dependency> 
    <groupId>javax.el</groupId> 
    <artifactId>javax.el-api</artifactId> 
    <version>2.2.4</version> 
</dependency> 
+1

爲什麼'hibernate-validator'不依賴那個依賴關係? –

+0

我不知道爲什麼,但它會更好 –

+0

@ thomas.mc.work我認爲,以避免傳遞依賴問題 – gstackoverflow

4

如果您正在使用Tomcat作爲服務器運行時,你會得到此錯誤測試(因爲tomcat運行時在測試期間是不可用的)比包含tomcat el運行時而不是來自glassfi的運行時有意義SH)。這將是:

<dependency> 
     <groupId>org.apache.tomcat</groupId> 
     <artifactId>tomcat-el-api</artifactId> 
     <version>8.5.14</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.tomcat</groupId> 
     <artifactId>tomcat-jasper-el</artifactId> 
     <version>8.5.14</version> 
     <scope>test</scope> 
    </dependency> 
5

關於Hibernate validator documentation page,你必須定義一個依賴於JSR-341實現:

<dependency> 
    <groupId>org.glassfish</groupId> 
    <artifactId>javax.el</artifactId> 
    <version>3.0.1-b08</version> 
</dependency> 
+0

謝謝@herau,救了我很多時間。 – Bodil