英文:
Springboot 3.0 Jakarta field validations
问题
在Spring Boot 3.0中不支持javax验证,如何使用Jakarta进行字段验证。
导入
import jakarta.persistence.Entity;
import jakarta.persistence.NotNull;
import jakarta.persistence.Pattern;
@Entity
@Data
public class Users {
@NotNull
@NotEmpty
Private String username;
@Pattern
Private String username;
@NotNull
Private String username;
}
Controller
public class UsersController {
public void saveUser(@Valid @RequestBody Users user){
repo.save();
}
}
英文:
As javax validations not supported in springboot 3.0. How to do field validations using Jakarta.
imports
import jakarta.persistence.Entity;
import jakarta.persistence.NotNull;
import jakarta.persistence.Pattern;
@Entity
@Data
public class Users{
@NotNull
@NotEmpty
Private String username;
@Pattern
Private String username;
@NotNull
Private String username;
}
Controller
public class UsersController{
public void saveUser(@Valid @RequestBody Users user){
repo.save();
}
}
答案1
得分: 3
你需要在你的 pom.xml
文件中添加 spring-boot-starter-validation
依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
英文:
You need to have spring-boot-starter-validation
dependency in your pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
答案2
得分: 0
添加以下的Maven依赖:
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
英文:
Add the following Maven dependency
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论