Spring Boot - Valid Form表单参数验证
本文介绍使用
@Valid
优雅的进行Form表单参数校验,避免大量的if(){...}
语句…
主要依赖
lombok插件主要为了简化代码,自行视情况添加使用。
thymeleaf主要为了页面测试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
创建实体类
创建一个Person
实体类:
@Data
public class Person {
@NotNull
@Size(min = 4, max = 16, message = "用户名须在4-16位之间")
private