简体   繁体   English

为什么@NotBlank 不显示为导入

[英]why isn't @NotBlank not showing up as an import

Having an issue with my code我的代码有问题

在此处输入图像描述 在此处输入图像描述 package com.example.demoSpring.student; package 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
    }

}

It's telling me that import javaz.validation.contraints.NotBlank doesn't exist, How do I install these on my IntelliJ它告诉我 import javaz.validation.contraints.NotBlank 不存在,如何在我的 IntelliJ 上安装这些

Im working on a project and I been having issues with this any import would be helpful, I'm using postman.我正在做一个项目,我遇到了这个问题,任何导入都会有帮助,我正在使用 postman。 it just every time I check import javax.只是每次我检查 import javax. validation .constraints.NotBlank;验证.constraints.NotBlank; is red I check options add dependency I get no result maven when I click find jar on web cant find it.是红色的,我检查选项添加依赖项当我在 web 上单击查找 jar 时,我没有得到结果 maven 找不到它。 How can I import this into my IntelliJ program?如何将其导入我的 IntelliJ 程序?

Also here is my pom.xml code这也是我的 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>

I added this我添加了这个

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

but the bold area is red dependency isn't found for these但是粗体区域是红色的,没有找到这些依赖项在此处输入图像描述

Im still getting an error when I try to input dependency当我尝试输入依赖项时仍然出现错误

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

For Spring Boot project, I added "validation-api" but postman was not throwing error and it allowed to store Blank fields (@NotBlank was not working).对于 Spring 引导项目,我添加了“validation-api”,但 postman 没有抛出错误,它允许存储空白字段(@NotBlank 不工作)。 Then I used this -然后我用这个 -

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

and it worked.它奏效了。

You need to add validation-api dependency你需要添加validation-api依赖

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM