spring启动bean验证集成时出错

j7dteeu8  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(382)

我正在尝试启动我的Web应用程序,但出现以下两个错误:

2020-12-22 20:46:25.769  INFO 23060 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-12-22 20:46:26.161 ERROR 23060 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
2020-12-22 20:46:26.162  WARN 23060 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
2020-12-22 20:46:26.162  INFO 23060 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-12-22 20:46:26.177  INFO 23060 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-12-22 20:46:26.179  INFO 23060 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-12-22 20:46:26.191  INFO 23060 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-22 20:46:26.207 ERROR 23060 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************

APPLICATION FAILED TO START

***************************

以下是最终错误输出的描述:
说明:
试图调用不存在的方法。尝试从以下位置进行:

javax.el.ELManager.getExpressionFactory(ELManager.java:38)

以下方法不存在:

javax.el.ELUtil.getExpressionFactory()Ljavax/el/ExpressionFactory;

该方法的类javax.el.eloutil可从以下位置获得:

jar:file:/C:/Users/chafy/Desktop/backend/lib/javax.servlet.jsp.jar!/javax/el/ELUtil.class
jar:file:/C:/Users/chafy/.m2/repository/org/glassfish/jakarta.el/3.0.3/jakarta.el-3.0.3.jar!/javax/el/ELUtil.class
jar:file:/C:/Users/chafy/.m2/repository/javax/el/javax.el-api/3.0.0/javax.el-api-3.0.0.jar!/javax/el/ELUtil.class
jar:file:/C:/Users/chafy/.m2/repository/org/glassfish/javax.el/3.0.0/javax.el-3.0.0.jar!/javax/el/ELUtil.class

已从以下位置加载类层次结构:

javax.el.ELUtil: file:/C:/Users/chafy/Desktop/backend/lib/javax.servlet.jsp.jar

行动:
更正应用程序的类路径,使其包含javax.el.il的单个兼容版本
这是我的依赖:

<dependencies>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>annotations</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.11.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <scope>runtime</scope>
            <version>0.11.2</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <scope>runtime</scope>
            <version>0.11.2</version>
        </dependency>
        <!-- TimeAgo related dependencies-->
        <dependency>
            <groupId>com.github.marlonlom</groupId>
            <artifactId>timeago</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>2.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>5.4.26.Final</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>

这里是我的application.properties:


# ===============================

# = DATA SOURCE

# ===============================

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/cuberil?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Charigan99./

# Keep the connection alive if idle for a long time (needed in production)

spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.initialization-mode=always

# ===============================

# = JPA / HIBERNATE

# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is

# stripped before adding them to the entity manager).

# Show or not log for each sql query

spring.jpa.show-sql=true

# Hibernate ddl auto (create, create-drop, update): with "update" the database

# schema will be automatically updated accordingly to java entities found in

# the project

spring.jpa.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

# Allows Hibernate to generate SQL optimized for a particular DBMS

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true

# Mail Properties

spring.mail.host=smtp.mailtrap.io
spring.mail.port=25
spring.mail.username=<your-smtp-username>
spring.mail.password=<your-smtp-password>
spring.mail.protocol=smtp

我试图通过添加一些依赖项来解决这个问题,但什么都没有。有些人说这是servlet版本问题,但事实并非如此。

9fkzdhlc

9fkzdhlc1#

所以,我不知道确切的问题,但也许我可以给你一些东西看看和/或尝试。
通常,servlet、jsp和el库应该由容器提供。所以,我会考虑,加上 <scope>provided</scope> 到不同的servlet库:

<!-- Provided by Tomcat Container -->
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
    <scope>provided</scope>
</dependency>

既然您使用的是tomcat,我就不直接使用 org.glassfish.javax.el 图书馆。我认为每个容器都提供了自己的el库实现,如果有第二个库可能会导致类路径问题。
(很可能)从pom.xml中删除:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.el</artifactId>
    <version>3.0.0</version>
</dependency>

hibernate验证程序也有点麻烦。从这个类似问题的答案来看:
HibernateValidator6.x->BeanValidation2.0(JSR380)->el3.0
HibernateValidator5.x->BeanValidation1.1(JSR349)->el2.2
BeanValidation1.0(JSR303)->(我不确定)
因此,理论上,您应该使用HibernateValidator6,而不是像现在这样使用Version5。

km0tfn4u

km0tfn4u2#

首先,您需要在mysql中创建一个名为“cuberil”的数据库。

相关问题