I'm starting to study JavaSpring and I want to validate the attributes of my class. I'm using this annotation: @NotEmpty to validate the fields.
However, I am receiving the following message: Cannot resolve symbol 'NotEmpty
I think it's a problem with the import since the following is happening when trying to import:
Cannot resolve symbol 'validation'
import javax.validation.constraints.NotEmpty;
I don't know what's wrong.
How can I resolve this so the annotation works.
The class follows:
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);
}
}
Here's the 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()
}
modern versions of Java libraries use jakarta
instead of javax
- try jakarta.validation.constraints.NotEmpty