Springboot 3.0 Jakarta字段验证

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

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:

&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;

答案2

得分: 0

添加以下的Maven依赖:

<dependency>
  <groupId>jakarta.validation</groupId>
  <artifactId>jakarta.validation-api</artifactId>
  <version>3.0.2</version>
</dependency>
英文:

Add the following Maven dependency

&lt;dependency&gt;
  &lt;groupId&gt;jakarta.validation&lt;/groupId&gt;
  &lt;artifactId&gt;jakarta.validation-api&lt;/artifactId&gt;
  &lt;version&gt;3.0.2&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2023年2月14日 20:27:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447820.html
匿名

发表评论

匿名网友

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

确定