1

我的代码有问题

在此处输入图像描述 在此处输入图像描述 包 com.example.demoSpring.student;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.lang.NonNull;

**import javax.validation.constraints.NotBlank;**
import java.util.UUID;

public class Students {
    private final UUID studentId;

    **@NotBlank**
    private final String firstName;

    **@NotBlank**
    private final String lastName;

    **@NotBlank**
    private final String email;

    @NonNull
    private final Gender gender;

    public Students(@JsonProperty("studentId") UUID studentId,
                    @JsonProperty("firstName") String firstName,
                    @JsonProperty("lastName") String lastName,
                    @JsonProperty("email") String email,
                    @JsonProperty("gender") Gender gender) {
        this.studentId = studentId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
    }

    public UUID getStudentId() {
        return studentId;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEmail() {
        return email;
    }

    public Gender getGender() {
        return gender;
    }

    @Override
    public String toString() {
        return "Students{" +
                "studentId=" + studentId +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", gender=" + gender +
                '}';
    }

    enum Gender{
        MALE,FEMALE
    }

}

它告诉我 import javaz.validation.contraints.NotBlank 不存在,如何在我的 IntelliJ 上安装这些

我正在做一个项目,我遇到了这个问题,任何导入都会有帮助,我正在使用邮递员。只是每次我检查 import javax. 验证.constraints.NotBlank;是红色的,我检查选项添加依赖项当我单击 web 上的查找 jar 时,我没有得到任何结果 maven 找不到它。如何将其导入我的 IntelliJ 程序?

这也是我的 pom.xml 代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demoSpring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demoSpring</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>



        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.16.1</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demoSpring</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我添加了这个

<dependency>
    <groupId>**javax.validation**</groupId>
    <artifactId>**validation-api**</artifactId>
    <version>**2.0.1.Final**</version>
</dependency>

但是粗体区域是红色的,没有找到这些依赖项 在此处输入图像描述

当我尝试输入依赖项时仍然出现错误

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>
4

2 回答 2

4

对于 Spring Boot 项目,我添加了“validation-api”,但邮递员没有抛出错误,它允许存储空白字段(@NotBlank 不起作用)。然后我用这个 -

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <version>2.4.1</version>
</dependency>

它奏效了。

于 2021-01-15T15:23:04.320 回答
2

你需要添加validation-api依赖

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>
于 2020-06-29T03:53:14.577 回答