Don't work import jakarta.validation.constraints.NotBlank;

Hi, i'm a student and actually i'm using Spring, but when i using @NotNull or @NotBlank didn't work i put the library for example: jakarta.validation.constraints.NotBlank; & jakarta.validation.constraints.NotNull; I'm also using Maven and in the post you can see the dependecies. Thank you, if you help me
10 Replies
JavaBot
JavaBot4mo ago
This post has been reserved for your question.
Hey @Eloreuy! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Eloreuy
EloreuyOP4mo ago
<dependencies> <!-- Spring Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Validation (Jakarta Validation) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>jakarta.validation</groupId> <artifactId>jakarta.validation-api</artifactId> <version>3.0.2</version> </dependency> <!-- Devtools --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- JSTL (Jakarta) --> <dependency> <groupId>jakarta.servlet.jsp.jstl</groupId> <artifactId>jakarta.servlet.jsp.jstl-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>jakarta.servlet.jsp.jstl</artifactId> <version>3.0.1</version> </dependency> <!-- JSP Compiler --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- Servlet API (Jakarta 6.0) --> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> <!-- Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> There're the dependecies of my pom.xml
ayylmao123xdd
ayylmao123xdd4mo ago
can you show the code where you use the annotations
Eloreuy
EloreuyOP4mo ago
​`​`​`​java
package com.gestionale.GestionaleAP;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public class Impiegato {

@NotNull(message = "L'ID non può essere nullo")
private Integer id;

@NotBlank(message = "Il nome non può essere vuoto")
@Size(min = 2, max = 50, message = "Il nome deve avere tra 2 e 50 caratteri")
private String nome;

@NotBlank(message = "Il cognome non può essere vuoto")
@Size(min = 2, max = 50, message = "Il cognome deve avere tra 2 e 50 caratteri")
private String cognome;

@NotNull(message = "Lo stipendio non può essere nullo")
private Double stipendio;

// Getters e Setters

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public String getCognome() {
return cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}

public Double getStipendio() {
return stipendio;
}

public void setStipendio(Double stipendio) {
this.stipendio = stipendio;
}
}
​`​`​`​
​`​`​`​java
package com.gestionale.GestionaleAP;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public class Impiegato {

@NotNull(message = "L'ID non può essere nullo")
private Integer id;

@NotBlank(message = "Il nome non può essere vuoto")
@Size(min = 2, max = 50, message = "Il nome deve avere tra 2 e 50 caratteri")
private String nome;

@NotBlank(message = "Il cognome non può essere vuoto")
@Size(min = 2, max = 50, message = "Il cognome deve avere tra 2 e 50 caratteri")
private String cognome;

@NotNull(message = "Lo stipendio non può essere nullo")
private Double stipendio;

// Getters e Setters

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public String getCognome() {
return cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}

public Double getStipendio() {
return stipendio;
}

public void setStipendio(Double stipendio) {
this.stipendio = stipendio;
}
}
​`​`​`​
Here
ayylmao123xdd
ayylmao123xdd4mo ago
and how is this class used in a controller or how exactly for the validation to work you need to use the @valid annotation in the method
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Eloreuy
EloreuyOP4mo ago
Yeah
package com.gestionale.GestionaleAP;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ImpiegatoCtr {

@RequestMapping("/insertIMP")
public void insert(Model m, @Valid @ModelAttribute("impForm") Impiegato imp, BindingResult r) {
m.addAttribute("impForm", new Impiegato());
if (r.hasErrors()) {
System.err.println("Errore di validazione");
}
}
}
package com.gestionale.GestionaleAP;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ImpiegatoCtr {

@RequestMapping("/insertIMP")
public void insert(Model m, @Valid @ModelAttribute("impForm") Impiegato imp, BindingResult r) {
m.addAttribute("impForm", new Impiegato());
if (r.hasErrors()) {
System.err.println("Errore di validazione");
}
}
}
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
ayylmao123xdd
ayylmao123xdd4mo ago
you are importing the validated annotation not the valid one
JavaBot
JavaBot4mo ago
Post Closed
This post has been closed by <@796803817424552008>.

Did you find this page helpful?