Bean Validation API在类路径上,但找不到任何实现,导致启动失败。

42

我正在使用Spring Tool Suite来处理Java中的ReST服务。但是在第一个关键点上,我无法启动我的第一个简单应用程序。请帮忙。我遇到了以下错误。[使用Java8,STS套件,Ubuntu 16.04,$Java_Home:/usr/lib/jvm/java-8-oracle]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.2.RELEASE)

2018-01-28 09:37:14.658  INFO 7971 --- [           main] i.j.springbootstarter.CourseApiApp       : Starting CourseApiApp on rudresh-Vostro-14-3468 with PID 7971 (/home/rudresh/Documents/workspace-sts-3.9.2.RELEASE/course-api/target/classes started by rudresh in /home/rudresh/Documents/workspace-sts-3.9.2.RELEASE/course-api)
2018-01-28 09:37:14.667  INFO 7971 --- [           main] i.j.springbootstarter.CourseApiApp       : No active profile set, falling back to default profiles: default
2018-01-28 09:37:14.827  INFO 7971 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6dde5c8c: startup date [Sun Jan 28 09:37:14 IST 2018]; root of context hierarchy
2018-01-28 09:37:16.551  WARN 7971 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration$StringHttpMessageConverterConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProperties': 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-28 09:37:16.552  INFO 7971 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2018-01-28 09:37:16.568  INFO 7971 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-28 09:37:16.578 ERROR 7971 --- [           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

以下是我的POM.xml文件。整个示例是从JavaBrains的YouTube教程中提取出来供参考目的。我怀疑存在一些问题,可能是环境或Hibernate验证,因为我没有在系统中安装它。

<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.4.2.RELEASE</version>
  </parent>
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

"This is main class to start SpringBoot application. Although it does not do anything but it should at least redirect to browser Error page."


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) {
        // TODO Auto-generated method stub
        SpringApplication.run(CourseApiApp.class, args);
    }

}

4
请在你的pom.xml文件中添加以下依赖项: org.hibernate hibernate-validator 这将使你的项目能够使用Hibernate验证器。 - Dipak Thoke
嗨Dipak,感谢您的评论。这次我也添加了那个标签,但问题仍然存在。我在系统中同时安装了Eclipse和STS套件。这可能是一个问题吗? - Rudresh Gaur
同时,通过使用spring-boot-starter-web,Spring Boot会下载Hibernate Validator依赖项。从Maven依赖列表中可以看到。 - Rudresh Gaur
但是你能在生成的jar文件中看到它吗? - eis
@RudreshGaur 从pom.xml中删除<start-class>com.bt.collab.alu.api.webapp.Application</start-class>,然后它应该可以工作。 - Dipak Thoke
显示剩余4条评论
4个回答

91
在我的情况下,我添加了以下依赖项并且它可以工作,即使我不需要任何验证,也许是Java更新引起了这个问题。
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

1
这是Spring官方的答复。 - Magnus
3
为什么启动的jar包里没有这个东西啊,哈哈?不太明白为什么这些东西非要搞得那么复杂! - Klaus Nji
你有任何想法为什么这不能解决问题吗? - Gewure
谢谢,这解决了我的错误。我在将Spring版本从2.3.1更新到2.6.7时遇到了这个错误。我还添加了javax-validation依赖以支持验证注释。 - Masoom Raza
这个依赖项在Gradle中的等效方式是什么? - HibaHasan

8

删除<user_home>/.m2/repository下的所有文件,然后尝试使用命令进行项目安装。

看起来是某个文件损坏了。因此,为了正确下载文件,请尝试执行此过程。

完全不需要添加其他依赖项。'web'注入了缺失的验证器。

我也遇到了同样的问题,并通过执行此过程解决了它。您只需要执行一次,然后就可以继续使用了。(有时候 - 别问我为什么 - 本地m2存储库会损坏)。

希望能帮到你。祝好,伊万


1
仅仅添加 spring-boot-starter-validation 并不足以让我的应用程序重新运行起来。 但是我并不需要删除整个本地 Maven 存储库。只需删除这个文件夹即可:~/.m2/repository/org/hibernate/validator - Ralf

5

我遇到了同样的问题。 我的项目已经在生产环境中运行了近一年,但这周我需要进行一些更改,结果惊奇地发现,项目甚至无法构建。 我通过将以下依赖项添加到我的项目中来解决了此问题。

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${your-desired-version-matching-project-own}</version>
</dependency>

注意:我正在使用Spring Starters依赖,但仍未加载该依赖项。

5
是的,这是最新版本的Spring Boot中的变更。验证(starter)不再包含在Web starters中。 - rougou

1

我知道这已经过时了,但以下方法对我有效。

注意Gradle

implementation(group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version:'2.7.8'
      exclude(module: 'spring-boot-starter-logging')
      exclude(module: 'spring-boot-starter-tomcat')

希望这能帮助到某人。

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