validation主要是校验用户提交的数据的合法性,比如是否为空,密码是否符合规则,邮箱格式是否正确等等,校验框架比较多,用的比较多的是hibernate-validator, 也支持国际化,也可以自定义校验类型的注解,这里只是简单的演示校验框架在Spring Boot中的简单集成,要想了解更多可以参考 hibernate-validator
1. pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2. dto
public class UserInfoIDto {
private Long id;
@NotBlank
@Length(min=3, max=10)
private String username;
@NotBlank
@Email
private String email;
@NotBlank
@Pattern(regexp="^((13[0-9])|(15[^4,\\D])|(18[0,3-9]))\\d{8}$", message