SpringBoot自带的校验组件
导入依赖
我这里的springboot的版本为3.4.0
<!--validation 校验依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
用法如下,只要在参数上写好参数校验就行
@PostMapping("/login")
public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password)
{
}
搭配注解使用
搭配注解使用更加丝滑
@Data
@Builder
public class User {
private Integer id;//主键ID
@NotEmpty
@Pattern(regexp = UserRegexUtils.USER_NAME_REGEX)
private String username;//用户名
@JsonIgnore
private String password;//密码
@Pattern(regexp = UserRegexUtils.USER_NICKNAME_REGEX)
@NotEmpty
private String nickname;//昵称
@NotEmpty
@Pattern(regexp = UserRegexUtils.USER_EMAIL_REGEX)
private String email;//邮箱
private