java.lang.NoSuchMethodError: javax.validation.BootstrapConfiguration.getClockProviderClassName

JDK: 1.8.0_131

雄猫: 8.0.27.0

休眠验证器: 6.0.7.最终 + 所有依赖项下载自: 休眠验证器 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;    
    }

代码在 上失败,并出现异常:Validation.buildDefaultValidatorFactory()

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)

看起来使用了错误的jar文件,但我无法弄清楚是哪一个。


答案 1

从Springboot 1.5.x升级到Springboot2后,我遇到了同样的问题。解决方案是将Java EE 7升级到Java EE 8:

从:

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

自:

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

答案 2

Spring Boot 2 附带了 hibernate-validator 6(取决于 validation-api 2 (),它特定于 Java EE 8,请参阅附录 F. 依赖版本。但是,您可能必须仅使用 Java EE 7 支持较旧的应用程序服务器。Spring Framework 5 应该仍然支持它,请参阅运行时支持org.hibernate.validator:hibernate-validator:6.0.16.Finaljavax.validation:validation-api:2.0.1.Final

在这种情况下,请使用较旧的休眠验证程序5.4.3.Final) 和 validation-api1.1.0.Final)。如果您使用 Spring Boot maven 父级,只需定义这些属性即可。

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

问题是休眠验证器已经更改了版本6以来的,因此您必须排除新组但添加旧组,例如groupId

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

推荐