简体   繁体   English

Spring启动示例:无法启动嵌入式容器错误

[英]Spring boot sample: Unable to start embedded container error

I am new to spring, so i downloaded jar from spring intializr for maven-web java 1.8 demo. 我是春天的新手,所以我从spring intializr下载jar用于maven-web java 1.8 demo。 I extracted and imported it to STS to run the main() file and I get below exception. 我解压缩并将其导入STS以运行main()文件,然后我得到以下异常。 Can some one tell me what's solution for this? 有人可以告诉我这是什么解决方案吗?

Solutions I tried from web but didn't work: - Tried adding Hibernate-validator dependency to pom - Tried adding spring-boot-starter-tomcat dependency to pom 解决方案我尝试从网络但没有工作: - 尝试将Hibernate验证器依赖添加到pom - 尝试将spring-boot-starter-tomcat依赖添加到pom

STACK TRACE: 堆栈跟踪:

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 的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>

Application file: 申请文件:

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);
    }
}

Had the same problem with release version 1.5.9 of Spring Boot. 与Spring Boot的1.5.9发行版有同样的问题。 To add to the answer by Jim Kiley , you should add the following in Maven pom.xml: 要添加Jim Kiley的答案,您应该在Maven pom.xml中添加以下内容:

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

You can use the dependency version under properties section of Spring Boot pom.xml file (spring-boot-dependencies-1.5.9.RELEASE.pom): 您可以使用Spring Boot pom.xml文件的属性部分下的依赖项版本(spring-boot-dependencies-1.5.9.RELEASE.pom):

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

Located under path: 位于路径下:

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

As the output says, you need to: 如输出所示,您需要:

Add an implementation, such as Hibernate Validator, to the classpath 将一个实现(如Hibernate Validator)添加到类路径中

Since you're using Maven, that means you need to add the Hibernate Validator to your pom.xml's dependencies. 由于您使用的是Maven,这意味着您需要将Hibernate Validator添加到pom.xml的依赖项中。

First of all, your pom.xml settings are correct. 首先,您的pom.xml设置是正确的。 spring-boot-starter-web has the dependency to hibernate-validator . spring-boot-starter-web依赖于hibernate-validator So in your project, you don't need to specify the dependency to hibernate-validator . 所以在你的项目中,你不需要指定对hibernate-validator的依赖。 Otherwise, what's the purpose to use maven? 否则,使用maven的目的是什么?

So back to the resolution, the steps I did to resolve the problem are: 回到解决方案,我为解决问题所采取的步骤是:

  1. Goto project folder where the pom.xml is located. 转到pom.xml所在的项目文件夹。
  2. Run mvn verify The command will download the missing jar again from the repository and also check the correctness(corrupted or not) of jars in maven local repository. 运行mvn verify该命令将再次从存储库下载缺少的jar,并检查maven本地存储库中jar的正确性(是否已损坏)。

    If you see the warning messages similar to following: 如果您看到类似以下的警告消息:

    [WARNING] error reading /home/vagrant/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.6/tomcat-embed-core-8.5.6.jar; [警告]错误读取/home/vagrant/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.6/tomcat-embed-core-8.5.6.jar; invalid LOC header (bad signature) LOC标头无效(签名错误)

    [WARNING] error reading /home/vagrant/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar; [警告]错误读取/home/vagrant/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar; invalid LOC header (bad signature) LOC标头无效(签名错误)

    [WARNING] error reading /home/vagrant/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.4/jackson-core-2.8.4.jar; [警告]错误读取/home/vagrant/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.4/jackson-core-2.8.4.jar; invalid LOC header (bad signature) LOC标头无效(签名错误)

    Then this means that the jars downloaded by maven previously has been corrupted. 那么这意味着maven以前下载的罐子已经损坏了。

  3. To correct this, please delete those corrupted jars and run mvn verify again to re-download them and also ensure that there's no warning for jars. 要更正此问题,请删除那些损坏的jar并再次运行mvn verify以重新下载它们,并确保没有对jar的警告。

  4. Go back to your IDE(STS or Eclipse), update your project and run the application again. 返回IDE(STS或Eclipse),更新项目并再次运行应用程序。

In above error the hibernate-validator-5.2.4.Final.jar is the hibernate validator that in quesion. 在上面的错误中, hibernate-validator-5.2.4.Final.jar是问题的hibernate验证器。

I met same issue and resolved it by delete these two folders under .m2 我遇到了同样的问题并通过删除.m2下的这两个文件夹来解决它

  • com\\fasterxml --> delete it will resolve TypeResovler issue. com \\ fasterxml - >删除它将解决TypeResovler问题。
  • org\\springframework --> delete it will resolve lack of validator issue. org \\ springframework - >删除它将解决缺少验证器问题。

then the program works. 然后程序工作。

I faced the same issue while runs the application and resolved the issue by deleting the .m2 folder. 我在运行应用程序时遇到了同样的问题,并通过删除.m2文件夹解决了问题。

.m2/repository .m2目录/库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM