将一个实现(例如Hibernate Validator)添加到类路径错误中使用SpringBoot

7

我是Spring Boot的新手,建立了我的第一个简单项目,但在运行应用程序时遇到了问题。

请问为什么会出现下面的错误?

pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.javabrains.springbootquickstart</groupId>
  <artifactId>course-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>java Brains Course API</name>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
  </dependencies>
  <properties>
    <java.version>1.8</java.version>
  </properties>
</project> 

CourseApiApp.java

    package io.javabrains.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApiApp {

    public static void main(String[] args) {
        SpringApplication.run(CourseApiApp.class, args);

    }

}

这个屏幕截图属于我的示例。

enter image description here

应用程序启动失败。 "添加一个实现,例如Hibernate Validator,到类路径中"。
Console output below: 应用程序启动失败。 "添加一个实现,例如Hibernate Validator,到类路径中"。
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.1.RELEASE)

2018-01-09 11:31:47.216  INFO 12224 --- [           main] i.j.springbootstarter.CourseApiApp       : Starting CourseApiApp on kafein-kafein with PID 12224 (C:\Users\kafein\Documents\workspace-sts-3.9.2.RELEASE\course-api\target\classes started by kafein in C:\Users\kafein\Documents\workspace-sts-3.9.2.RELEASE\course-api)
2018-01-09 11:31:47.223  INFO 12224 --- [           main] i.j.springbootstarter.CourseApiApp       : No active profile set, falling back to default profiles: default
2018-01-09 11:31:47.314  INFO 12224 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a8448fa: startup date [Tue Jan 09 11:31:47 EET 2018]; root of context hierarchy
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/C:/Users/kafein/.m2/repository/org/springframework/spring-core/4.3.6.RELEASE/spring-core-4.3.6.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2018-01-09 11:31:48.148  WARN 12224 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'standardJacksonObjectMapperBuilderCustomizer' defined in class path resource [org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$Jackson2ObjectMapperBuilderCustomizerConfiguration.class]: Unsatisfied dependency expressed through method 'standardJacksonObjectMapperBuilderCustomizer' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties': Initialization of bean failed; nested exception is javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
2018-01-09 11:31:48.156  INFO 12224 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-09 11:31:48.162 ERROR 12224 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Bean Validation API is on the classpath but no implementation could be found

Action:

Add an implementation, such as Hibernate Validator, to the classpath
5个回答

6
你缺少一些库。在你的pom.xml中添加以下Spring Boot依赖项即可解决问题。Spring Boot会在你的项目中找到正确的实现。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

另外,添加Hibernate依赖项:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Hibernate -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-ehcache</artifactId>
  <exclusions>
    <exclusion>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-java8</artifactId>
</dependency>

1
我添加了这个,但还是一样的。 - nurdan karaman
谢谢,但是没有任何改变 :s - nurdan karaman
尝试从您的pom中删除hibernate-validator依赖项。我没有使用它。Spring Boot会自动添加它。 - viniciusjssouza
添加 spring-boot-starter-validation 依赖项解决了我的问题! - GedankenNebel
Ehcache的依赖关系与此无关。 - The Cloud Guy

5
根据此链接,最新的Spring Boot版本应该已经修复了此问题:https://github.com/spring-projects/spring-boot/pull/12669(允许在类路径中使用验证API而无需实现)。对于旧的Boot版本,在使用spring-boot-starter web时,解决此异常的另一种方法是排除验证API。例如:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>        
        <exclusion>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1
我只需要添加以下内容: compile 'org.springframework.boot:spring-boot-starter-validation'

1

我正在使用Spring Boot 2.4.4版本。最初,我使用了下面的Spring验证器依赖项,但它没有起作用。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

但后来,我使用了下面这个Hibernate依赖项,在我的代码中完美地工作了。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.2.Final</version>
</dependency>

0
是的,@viniciusjssouza的答案对我有用。我的Spring Boot版本是2.7。 即使你没有Hibernte.Validator,只需将此依赖项添加到Gradle或类似于Maven的工具中。
实现 'org.springframework.boot:spring-boot-starter-validation'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接