【问题标题】:Spring boot sample: Unable to start embedded container errorSpring Boot 示例:无法启动嵌入式容器错误
【发布时间】:2017-01-26 17:35:59
【问题描述】:

我是 spring 新手,所以我从 spring intializr 下载了用于 maven-web java 1.8 演示的 jar。我将其提取并导入到 STS 以运行 main() 文件,我得到以下异常。有人可以告诉我有什么解决方案吗?

我从网络尝试过的解决方案,但不起作用: - 尝试将 Hibernate-validator 依赖项添加到 pom - 尝试将 spring-boot-starter-tomcat 依赖添加到 pom

堆栈跟踪:

2017-01-26 12:19:32.587  INFO 3816 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2e4b8173: startup date [Thu Jan 26 12:19:32 EST 2017]; root of context hierarchy
2017-01-26 12:19:36.442  WARN 3816 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration': 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.
2017-01-26 12:19:36.454 ERROR 3816 --- [           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

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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>

申请文件:

package com.example;

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

@SpringBootApplication
public class DemoApplication {

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

【问题讨论】:

  • Hibernate Validator 应该由 spring-boot-starter-web 传递地提供。我猜想当 Maven 下载一个 jar 时它已损坏。尝试清除 Maven 的缓存,然后重新构建您的应用
  • 谢谢@AndyWilkinson。我尝试使用 maven clean 进行重建,但得到了相同的结果。然后我从 .m2 文件夹中删除了所有文件夹(因为我长期使用 maven,所以我有很多文件夹)然后重新运行项目并且它工作正常。我想问题可能出在现有的 jar 版本上。
  • 我还必须删除所有内容并重新开始。简单地在 pom 文件中添加所需的依赖项没有任何作用。
  • 我也有同样的问题,你可以试试这个this

标签: spring spring-boot


【解决方案1】:

在 Spring Boot 的 1.5.9 版本中遇到了同样的问题。 要添加Jim Kiley 的答案,您应该在 Maven pom.xml 中添加以下内容:

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

您可以使用 Spring Boot pom.xml 文件 (spring-boot-dependencies-1.5.9.RELEASE.pom) 属性部分下的依赖版本:

<hibernate-validator.version>5.3.6.Final</hibernate-validator.version>

位于路径下:

/.m2/repository/org/springframework/boot/spring-boot-dependencies/1.5.9.RELEASE/spring-boot-dependencies-1.5.9.RELEASE.pom

【讨论】:

  • 这是我的问题的正确解释和答案。 :+1:
【解决方案2】:

正如输出所说,您需要:

在类路径中添加一个实现,例如 Hibernate Validator

由于您使用的是 Maven,这意味着您需要将 Hibernate Validator 添加到您的 pom.xml 的依赖项中。

【讨论】:

    【解决方案3】:

    首先,您的pom.xml 设置是正确的。 spring-boot-starter-web 依赖于 hibernate-validator。 所以在你的项目中,你不需要指定对hibernate-validator的依赖。不然用maven的目的是什么?

    所以回到解决方案,我为解决问题所做的步骤是:

    1. 转到pom.xml所在的项目文件夹。
    2. 运行mvn verify 该命令将从存储库中再次下载丢失的 jar,并检查 maven 本地存储库中 jar 的正确性(是否损坏)。

      如果您看到类似于以下的警告消息:

      [警告] 读取 /home/vagrant/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.6/tomcat-embed-core-8.5.6.jar 时出错;无效的 LOC 标头(错误的签名)

      [警告] 读取 /home/vagrant/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar 时出错;无效的 LOC 标头(错误的签名)

      [警告] 读取 /home/vagrant/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.4/jackson-core-2.8.4.jar 时出错;无效的 LOC 标头(错误的签名)

      那么这就说明之前maven下载的jar已经损坏了。

    3. 要更正此问题,请删除那些损坏的 jar 并再次运行 mvn verify 以重新下载它们,并确保没有 jar 警告。

    4. 返回您的 IDE(STS 或 Eclipse),更新您的项目并再次运行应用程序。

    在上述错误中,hibernate-validator-5.2.4.Final.jar 是有问题的休眠验证器。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题并通过删除 .m2 下的这两个文件夹来解决它

      • com\fasterxml --> 删除它将解决 TypeResovler 问题。
      • org\springframework --> 删除它会解决缺少验证器的问题 问题。

      然后程序运行。

      【讨论】:

        【解决方案5】:

        我在运行应用程序时遇到了同样的问题,并通过删除 .m2 文件夹解决了该问题。

        .m2/存储库

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-20
          • 2020-03-25
          • 2016-08-19
          • 1970-01-01
          • 2016-12-29
          • 2013-09-25
          • 2015-09-06
          相关资源
          最近更新 更多