JDK:1.8.0_131
Tomcat:8.0.27.0
Hibernate Validator:6.0.7.Final +从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;
}
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文件,但我无法弄清楚哪一个。
答案 0 :(得分:6)
Spring Boot 2随附了 hibernate-validator 6 (org.hibernate.validator:hibernate-validator:6.0.16.Final
),它取决于 validation-api 2 (javax.validation:validation-api:2.0.1.Final
),特定于Java EE 8,请参见Appendix F. Dependency versions。但是可能必须仅使用Java EE 7支持较旧的应用程序服务器。 Spring Framework 5 仍应支持它,请参见{{3 }}。
在这种情况下,请使用较旧的 hibernate-validator ( 5.4.3.Final )和 validation-api ( 1.1。 0.Final )。如果您使用Spring Boot maven parent,则只需定义这些属性。
<properties>
<javax-validation.version>1.1.0.Final</javax-validation.version>
<hibernate-validator.version>5.4.3.Final</hibernate-validator.version>
</properties>
问题是 hibernate-validator 从版本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>
答案 1 :(得分:2)
从Springboot 1.5.x升级到Springboot2后,我遇到了同样的问题。解决方案是将Java EE 7升级到Java EE 8:
发件人:
handleEdit = (comment) => {
this.setState({ comment: comment.text, comment_id: comment.id })
};
收件人:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
答案 2 :(得分:1)
我刚刚将 validation-api 依赖项添加到我的 pom.xml
中,并且可以正常工作!
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>