问题描述
我正在尝试使用休眠验证,但可以编译代码,但是启动失败时出现错误:
线程 main中的异常javax.validation.ValidationException:HV000183:无法初始化'javax.el.ExpressionFactory'。检查类路径上是否具有EL依赖项,或改用ParameterMessageInterpolator
I'm trying to use hibernate validation, code compiles, but when launched in fails with error:Exception in thread "main" javax.validation.ValidationException: HV000183: Unable to initialize 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead
代码:
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
public class Run {
public static void main(String[] args) {
User u = new User();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator v = factory.getValidator();
Set<ConstraintViolation<User>> result = v.validate(u);
System.out.println(result.size());
}
}
用户类别:
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
public class User {
@Pattern(regexp="[a-zA-Z0-9_]{5,10}", message="Username must be 5 to 10 characters long, " +
"only digits, letters and underscore permitted.")
@XmlAttribute(name = "username", required = true)
protected String username;
@Pattern(regexp="[0-9a-zA-Z]{5,10}", message="Password must be 5 to 10 characters long, " +
"only digits and letters permitted.")
@XmlAttribute(name = "passwd", required = true)
protected String passwd;
@NotNull
@XmlAttribute(name = "authToken", required = true)
protected String authToken;
Getters and setters...
pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.home.project1</groupId>
<artifactId>webapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.9</version>
<scope>test</scope>
</dependency>
<!-- SERVLET -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>runtime</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.0.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>5.4.0.Final</version>
<scope>runtime</scope>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
<scope>runtime</scope>
</dependency>
</dependencies>
我在,并使用完全相同的依赖项,还尝试在(没有任何春季启动代码,仅依赖于 spring-boot-starter-tomcat
而没有部署到servlet容器)。还尝试过《 Beginning Spring》一书中的依赖性:
I following guide at http://hibernate.org/validator/documentation/getting-started/ and use exactly same dependencies, also tried with dependencies in post Running Spring Boot within IntelliJ results in Unable to load 'javax.el.ExpressionFactory' (without any spring boot code, just with spring-boot-starter-tomcat
in dependencies and without deploying to servlet container). Also tried dependencies from book "Beginning Spring":
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
涉及完全相同的错误,但并不能解决问题。
which concerns exactly same error, but it didn't solve the problem.
推荐答案
您有2个不兼容的javax.el版本(3和2)。
You have 2 different incompatible versions of javax.el (3 and 2).
只需使用此依赖项,它将涵盖所有内容(这是我们在GitHub存储库的README.md中建议的依赖项,但我需要更新入门指南):
Just use this dependency, which will cover everything (it's the one we recommend in the GitHub repo's README.md but I need to update the getting started guide):
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b08</version>
</dependency>
并删除所有其他javax.el依赖项(API和impl)。完成后,它应该像一种魅力。
and remove all the other javax.el dependencies (API and impl). It should work like a charm once you're done.
这篇关于休眠验证“无法初始化javax.el.ExpressionFactory”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!