mumei2018 发表于 2024-4-13 02:57:38

无法import javax.validation.constraints.Email;



build.gradle里的内如下
plugins {
        id 'java'
        id 'org.springframework.boot' version '3.2.4'
        id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
        sourceCompatibility = '17'
}

repositories {
        mavenCentral()
}

dependencies {
        implementation 'javax.validation:validation-api:2.0.1.Final'
        implementation 'org.springframework.boot:spring-boot-starter-jdbc'
        implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
        implementation 'org.springframework.boot:spring-boot-starter-web-services'
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
        runtimeOnly 'com.h2database:h2'
        implementation 'org.hibernate.validator:hibernate-validator:6.2.0.Final'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
        useJUnitPlatform()
}

想要
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
出现报错
The import javax.validation cannot be resolved

请问是什么问题 我该怎么办

FishC 发表于 2024-4-13 02:57:51

您在项目的`build.gradle`文件中已经添加了`javax.validation:validation-api`的依赖,但是仍然出现"import javax.validation cannot be resolved"的错误。这个问题可能是由于缺少其他的依赖引起的。

您可以尝试添加`javax.el:javax.el-api`和`org.hibernate:hibernate-validator`的依赖来解决这个问题。修改你的`build.gradle`文件如下:

dependencies {
    implementation 'javax.validation:validation-api:2.0.1.Final'
    implementation 'javax.el:javax.el-api:3.0.0'
    implementation 'org.hibernate.validator:hibernate-validator:6.2.0.Final'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web-services'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

然后重新构建项目,看看是否解决了这个问题。希望对您有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 无法import javax.validation.constraints.Email;