Hibernate 验证“无法初始化 javax.el.E​​xpressionFactory"错误 [英] Hibernate validation "Unable to initialize javax.el.ExpressionFactory" error

查看:36
本文介绍了Hibernate 验证“无法初始化 javax.el.E​​xpressionFactory"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用休眠验证,代码编译,但在启动时失败并出现错误:线程主"javax.validation.ValidationException 中的异常:HV000183:无法初始化javax.el.E​​xpressionFactory".检查类路径上是否有 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>

我遵循 http://hibernate.org/validator/documentation/getting-started 的指南/ 并使用完全相同的依赖项,也尝试使用 post 在 IntelliJ 中运行 Spring Boot 导致 Unable to load 'javax.el.E​​xpressionFactory' (没有任何 Spring Boot 代码,只需使用 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.

这篇关于Hibernate 验证“无法初始化 javax.el.E​​xpressionFactory"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆