为什么 `@NotBlank` 没有显示为导入?

huangapple go评论84阅读模式
英文:

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

问题

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
    }
}
<!-- pom.xml -->
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

Please note that the images and non-code content from the original text have been omitted as per your request. If you need further assistance, feel free to ask.

英文:

Having an issue with my code

为什么 `@NotBlank` 没有显示为导入?
为什么 `@NotBlank` 没有显示为导入?
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(&quot;studentId&quot;) UUID studentId,
@JsonProperty(&quot;firstName&quot;) String firstName,
@JsonProperty(&quot;lastName&quot;) String lastName,
@JsonProperty(&quot;email&quot;) String email,
@JsonProperty(&quot;gender&quot;) 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 &quot;Students{&quot; +
&quot;studentId=&quot; + studentId +
&quot;, firstName=&#39;&quot; + firstName + &#39;\&#39;&#39; +
&quot;, lastName=&#39;&quot; + lastName + &#39;\&#39;&#39; +
&quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
&quot;, gender=&quot; + gender +
&#39;}&#39;;
}
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

Im working on a project and I been having issues with this any import would be helpful, I'm using postman.
it just every time I check import javax.validation.constraints.NotBlank; is red I check options add dependency I get no result maven when I click find jar on web cant find it. How can I import this into my IntelliJ program?

Also here is my pom.xml code

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.0.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;demoSpring&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;demoSpring&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jdbc&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.flywaydb&lt;/groupId&gt;
&lt;artifactId&gt;flyway-core&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.postgresql&lt;/groupId&gt;
&lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.assertj/assertj-core --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.assertj&lt;/groupId&gt;
&lt;artifactId&gt;assertj-core&lt;/artifactId&gt;
&lt;version&gt;3.16.1&lt;/version&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
&lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
&lt;version&gt;3.4.5&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;demoSpring&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;commons-validator&lt;/groupId&gt;
&lt;artifactId&gt;commons-validator&lt;/artifactId&gt;
&lt;version&gt;1.3.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

I added this

&lt;dependency&gt;
&lt;groupId&gt;**javax.validation**&lt;/groupId&gt;
&lt;artifactId&gt;**validation-api**&lt;/artifactId&gt;
&lt;version&gt;**2.0.1.Final**&lt;/version&gt;
&lt;/dependency&gt;

but the bold area is red dependency isn't found for these
为什么 `@NotBlank` 没有显示为导入?

Im still getting an error when I try to input dependency

&lt;dependency&gt;
&lt;groupId&gt;javax.validation&lt;/groupId&gt;
&lt;artifactId&gt;validation-api&lt;/artifactId&gt;
&lt;version&gt;2.0.1.Final&lt;/version&gt;
&lt;/dependency&gt;

答案1

得分: 6

关于 Spring Boot 项目,我添加了 "validation-api" 但 Postman 没有抛出错误,它允许存储空白字段(@NotBlank 不起作用)。然后我使用了以下内容 -

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>2.4.1</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).
Then I used this -

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;version&gt;2.4.1&lt;/version&gt;
&lt;/dependency&gt;

and it worked.

答案2

得分: 3

需要添加validation-api依赖

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

You need to add validation-api dependency

&lt;dependency&gt;
&lt;groupId&gt;javax.validation&lt;/groupId&gt;
&lt;artifactId&gt;validation-api&lt;/artifactId&gt;
&lt;version&gt;2.0.1.Final&lt;/version&gt;
&lt;/dependency&gt;

答案3

得分: 1

你只需要添加这个依赖:

<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-validation</artifactId> 
</dependency>
英文:

you just need to add this dependency:

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年6月29日 11:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62630887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定