当前位置: 首页 > 知识库问答 >
问题:

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

阳狐若
2023-03-14

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

我从Web尝试但不起作用的解决方案:-尝试将Hibernate验证器依赖项添加到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);
    }
}

共有3个答案

冯庆
2023-03-14

首先,您的pom。xml设置正确<代码>spring boot starter web依赖于Hibernate验证器。因此,在您的项目中,不需要指定对hibernate validator的依赖关系。否则,使用maven的目的是什么?

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

>

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

[警告]读取/家庭/流浪者/时出错。m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.6/tomcat-embed-core-8.5.6。罐子无效的LOC标头(签名错误)

[警告]读取/家庭/流浪者/时出错。m2/repository/org/hibernate/hibernate validator/5.2.4。最终/hibernate-validator-5.2.4。最终的罐子无效的LOC标头(签名错误)

[警告]读取/家庭/流浪者/时出错。m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.4/jackson-core-2.8.4。罐子无效的LOC标头(签名错误)

那么这意味着maven之前下载的罐子已经损坏了。

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

在上面的错误中,hibernate-validator-5.2.4。Final.jar是qusion中的hibernate验证器。

袁建木
2023-03-14

正如输出所述,您需要:

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

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

岳正阳
2023-03-14

Spring Boot的1.5.9版本也有同样的问题。要添加Jim Kiley的答案,您应该在Mavenpom.xml中添加以下内容:

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

您可以在Spring Boot pom的properties部分下使用依赖关系版本。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
 类似资料: