带有未知@PastOrPresent注释的Spring-boot应用程序

时间:2018-01-23 17:55:20

标签: spring-boot bean-validation

在spring-boot网络应用中, 当我使用@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

1 个答案:

答案 0 :(得分:1)

问题是@PastOrPresent约束是Bean Validation 2.0的一部分(参见here),而Spring Boot默认仍然使用Hibernate Validator 5.3(参见依赖关系here ),这是BeanValidation 1.1。如果您想使用此新约束,则需要对Hibernate Validator 6.0具有显式依赖关系,并从当前依赖关系中排除5.4版本。