javax.validation.ValidationException:HV000183:无法加载“javax.el.E​​xpressionFactory”

我尝试用hibernatevalidation器编写非常简单的应用程序:

我的步骤:

在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); } } 

我得到以下stacktrace:

 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 

我错了什么?

它在添加到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> 

Hibernate Validator入门

做正义

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

关于Hibernatevalidation器文档页面 ,你必须定义一个JSR-341实现的依赖关系:

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

如果你正在使用tomcat作为你的服务器运行时,并且在testing中得到这个错误(因为tomcat运行时在testing期间是不可用的),那么包含tomcat el运行时而不是glassfish中的那个是有意义的。 这将是:

  <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>