4

I am new to spring, so i downloaded jar from spring intializr for maven-web java 1.8 demo. I extracted and imported it to STS to run the main() file and I get below exception. 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

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

<?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);
    }
}
Avinash
  • 4,115
  • 2
  • 22
  • 41
Anusha Kota
  • 41
  • 1
  • 1
  • 3
  • Hibernate Validator should be provided transitively by `spring-boot-starter-web`. I'd guess that a jar was corrupted when Maven downloaded it. Try clearing Maven's cache and then rebuild your app – Andy Wilkinson Jan 27 '17 at 00:04
  • Thanks @AndyWilkinson. I tried using maven clean to rebuild but got the same result. Then I deleted all folders from .m2 folder(as I was using maven for long I had a lot of folders) then re-ran the project and it worked. I guess the issue could be with existing jar versions. – Anusha Kota Jan 27 '17 at 19:00
  • I also had to remove everything and start fresh. Simply adding the required dependencies in the pom file did nothing. – James May 28 '17 at 01:55
  • I had same issue, you can try this [this](https://stackoverflow.com/a/51466824/8586437) – Rayon Jul 22 '18 at 16:03

5 Answers5

8

Had the same problem with release version 1.5.9 of Spring Boot. To add to the answer by Jim Kiley, you should add the following in 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):

<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
harmonious
  • 521
  • 8
  • 8
3

As the output says, you need to:

Add an implementation, such as Hibernate Validator, to the classpath

Since you're using Maven, that means you need to add the Hibernate Validator to your pom.xml's dependencies.

Jim Kiley
  • 3,632
  • 3
  • 26
  • 43
3

First of all, your pom.xml settings are correct. spring-boot-starter-web has the dependency to hibernate-validator. So in your project, you don't need to specify the dependency to hibernate-validator. Otherwise, what's the purpose to use 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.
  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.

    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; invalid LOC header (bad signature)

    [WARNING] error reading /home/vagrant/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar; invalid LOC header (bad signature)

    [WARNING] error reading /home/vagrant/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.4/jackson-core-2.8.4.jar; invalid LOC header (bad signature)

    Then this means that the jars downloaded by maven previously has been corrupted.

  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.

  4. Go back to your IDE(STS or Eclipse), update your project and run the application again.

In above error the hibernate-validator-5.2.4.Final.jar is the hibernate validator that in quesion.

Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52
Roy Wu
  • 31
  • 2
0

I met same issue and resolved it by delete these two folders under .m2

  • com\fasterxml --> delete it will resolve TypeResovler issue.
  • org\springframework --> delete it will resolve lack of validator issue.

then the program works.

jeri
  • 1
  • 2
0

I faced the same issue while runs the application and resolved the issue by deleting the .m2 folder.

.m2/repository

Naresh
  • 57
  • 1
  • 6