提问者:小点点

Hibernate验证“无法初始化javax. el.ExpressionFactory”错误


我正在尝试使用Hibernate验证,代码编译,但在启动时失败并出现错误:线程“main”javax. validation.ValidationException:HV000183:无法初始化“javax.el.ExpressionFactory”。检查您是否具有类路径上的EL依赖项,或改用ParameterMessageInterpolator

代码:

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>

我遵循http://hibernate.org/validator/documentation/getting-started/指南并使用完全相同的依赖项,也尝试了IntelliJ中运行Spring Boot后的依赖项,导致无法加载'javax. el.ExpressionFactory'(没有任何Spring Boot代码,只是在依赖项中使用spring-boot-starter-tomcat,没有部署到servlet容器)。还尝试了《开始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>

这涉及完全相同的错误,但并没有解决问题。


共2个答案

匿名用户

您有2个不同的不兼容版本的javax. el(3和2)。

只需使用此依赖项,它将涵盖所有内容(这是我们在GitHub存储库中推荐的README.md但我需要更新入门指南):

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

并删除所有其他javax. el依赖项(API和impl)。完成后,它应该会像魅力一样工作。

匿名用户