休眠验证 "Unable to initialize javax.el.ExpressionFactory" 错误

Hibernate validation "Unable to initialize javax.el.ExpressionFactory" error

我正在尝试使用 hibernate 验证、代码编译,但在启动时失败并出现错误: 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());
    }
}

用户class:

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/ and use exactly same dependencies, also tried with dependencies in post Running Spring Boot within IntelliJ results in Unable to load 'javax.el.ExpressionFactory' 的指南进行操作(没有任何 spring 引导代码,只有 spring-boot-starter-tomcat 依赖项并且没有部署到 servlet 容器)。还尝试了书中的依赖项 "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>

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

您有 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)。一旦你完成,它应该像魅力一样工作。

None 以上对我有用 然而这确实: 右键单击项目 > 属性 > Project Facets 在最右边的区域 select Runtimes 选项卡并选择您的 Tomcat Runtime,这将提供可用于测试的所需库。