27

JDK: 1.8.0_131

Tomcat: 8.0.27.0

Hibernate Validator: 6.0.7.Final + all the dependencies downloaded from: Hibernate Validator 6

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AccountSyncResponse excute(AccountSyncRequest account_sync_request_) 
    {        
       ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
       Validator validator = factory.getValidator();
       Set<ConstraintViolation<AccountSyncRequest>> violations = validator.validate(account_sync_request_);

       .
       .
       .
       .

       AccountSyncResponse _AccountSyncResponse = new AccountSyncResponse();

       return _AccountSyncResponse;    
    }

The code fail on Validation.buildDefaultValidatorFactory() with the exception:

java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String;
    at org.hibernate.validator.internal.xml.ValidationBootstrapParameters.<init>(ValidationBootstrapParameters.java:63)
    at org.hibernate.validator.internal.engine.ConfigurationImpl.parseValidationXml(ConfigurationImpl.java:527)
    at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:328)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:110)

It looks like the wrong jar file is being used but I can’t figure out which one.

asabd
  • 299
  • 1
  • 5
  • 9
  • 3
    Check if this answer helps you: https://stackoverflow.com/questions/47947293/maven-using-wrong-version-of-javax-validation/49693042#49693042 – Lasneyx Apr 06 '18 at 12:41
  • Hi @asabd, how did you fix this issue? I have a similar error: Error creating bean with name 'defaultValidator' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String; – user674669 Apr 23 '18 at 20:14
  • I got the same exception: java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName()Ljava/lang/String; at org.hibernate.validator.internal.xml.ValidationBootstrapParameters.(ValidationBootstrapParameters.java:61) with tags are [maven] [java ee]. – Oleksii Kyslytsyn Jun 13 '18 at 16:13

3 Answers3

19

I had this same issue after upgrading from Springboot 1.5.x to Springboot2. The solution was to upgrade Java EE 7 to Java EE 8:

From:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

To:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>8.0</version>
    <scope>provided</scope>
</dependency>
Chris Ritchie
  • 4,749
  • 2
  • 36
  • 33
  • The version 8 should not be needed. See _The Java EE 7 API level is required in Spring's corresponding modules now, with runtime support for the EE 8 level_ https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-5.x#baseline-update – banterCZ Jan 28 '20 at 08:40
  • In upgrading an application, I have come across a closely related issue, I was getting the Exception in the title. The change in the pom I needed was `javax:javaee-web-api:jar:7.0:provided` to `javax:javaee-web-api:jar:8.0:provided` (I'm on Java 11). I now get a different Exception, but know how to deal with that! ... I think! – Bill Naylor Oct 07 '21 at 10:01
16

Spring Boot 2 comes with hibernate-validator 6 (org.hibernate.validator:hibernate-validator:6.0.16.Final, which depends on validation-api 2 (javax.validation:validation-api:2.0.1.Final), which is specific for Java EE 8, see Appendix F. Dependency versions. But it may happen that you have to support older application servers with Java EE 7 only. Spring Framework 5 should still suport it, see runtime support.

In that case, use older hibernate-validator (5.4.3.Final) and validation-api (1.1.0.Final). If you use Spring Boot maven parent, just define these properties.

<properties>
    <javax-validation.version>1.1.0.Final</javax-validation.version>
    <hibernate-validator.version>5.4.3.Final</hibernate-validator.version>
</properties>

The problem is that hibernate-validator has changed the groupId since version 6, so you have to exclude the new group but add the old one, e.g.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>${hibernate-validator.version}</version>
</dependency>
banterCZ
  • 1,551
  • 1
  • 22
  • 37
9

I've just added validation-api dependency to my pom.xml and it works!

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>
Taras Melnyk
  • 3,057
  • 3
  • 38
  • 34
  • 1
    This worked for me. Some transitive dependency polluted my classpath with a 1.x version of the validation-api. Explicit declaration of 2.0.2 solved the dependency eviction race in sbt. – aelgn May 18 '21 at 15:28