问题
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.
回答1:
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>
回答2:
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>
来源:https://stackoverflow.com/questions/48323244/java-lang-nosuchmethoderror-javax-validation-bootstrapconfiguration-getclockpro