Spring-使用未知的@PastOrPresent 注释启动应用程序

Spring-boot app with unkown @PastOrPresent annotation

在spring-boot web-app, 当我使用@PastOrPresent STS 时显示此错误:

(PastOrPresent无法解析为类型)

但 org.springframework.boot:spring-boot-starter-validation 条目已经在依赖项中。

Gradle 文件

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.domain'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"

repositories {
    mavenCentral()
}


dependencies {

    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-actuator-docs')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-validation')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')
}

更新 Gradle 文件

...

compile('org.springframework.boot:spring-boot-starter-validation'){
 exclude group: 'org.hibernate', module: 'hibernate-validator:5.3.6.Final'
}
compile 'org.hibernate:hibernate-validator:6.0.7.Final'

...

|    \--- org.hibernate:hibernate-validator:5.3.6.Final -> 6.0.7.Final
|         \--- org.hibernate.validator:hibernate-validator:6.0.7.Final
|              +--- javax.validation:validation-api:2.0.1.Final -> 1.1.0.Final
|              +--- org.jboss.logging:jboss-logging:3.3.0.Final -> 3.3.1.Final
|              \--- com.fasterxml:classmate:1.3.1 -> 1.3.4

我还有事要做吗?在实体内部我仍然有同样的错误。使用新的:不推荐使用 NotEmpty 类型(如文档 here 所示)

问题是 @PastOrPresent 约束是 Bean Validation 2.0 的一部分(参见 here), while the Spring Boot is still using Hibernate Validator 5.3 by default (see the dependencies here),即 BeanValidation 1.1。如果您想使用这个新约束,您需要对 Hibernate Validator 6.0 具有显式依赖关系,并从当前依赖关系中排除 5.4 版本。