Я начинаю изучать JavaSpring и хочу проверить атрибуты моего класса.
Я использую эту аннотацию: @NotEmpty для проверки полей.
Однако я получаю следующее сообщение: Невозможно разрешить символ 'NotEmpty
Я думаю, что это проблема с импортом, поскольку при попытке импорта происходит следующее:
Невозможно разрешить «проверку» символа
import javax.validation.constraints.NotEmpty;
Я не знаю, что не так.
Как мне решить эту проблему, чтобы аннотация работала.
Класс следующий:
package models;
import javax.validation.constraints.NotEmpty;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Objects;
@Document(collection = "customers")
@Getter @Setter @ToString
public class Customer {
@Id
private String id;
@NotEmpty(message = "CPF is required")
private String cpf;
@NotEmpty(message = "Name is required")
private String name;
@NotEmpty(message = "Email is required")
private String email;
@NotEmpty(message = "Phone is required")
private String phone;
private Address address;
// Constructors
public Customer() {
}
public Cliente(String cpf, String name, String email, String phone, Address address) {
this.cpf = cpf;
this.name = name;
this.email = email;
this.phone = phone;
this.address = address;
}
// Equals and HashCode
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, client.id) && Objects.equals(cpf, client.cpf);
}
@Override
public int hashCode() {
return Objects.hash(id, cpf);
}
}
Вот build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.1'
id 'io.spring.dependency-management' version '1.1.5'
}
group = 'com.mf-clientes'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'javax.validation:validation-api:2.0.1.Final'
implementation 'org.hibernate.validator:hibernate-validator:6.1.5.Final'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... l-notempty