springboot参数校验最全使用 (springboot数据交互)

SpringBoot-9-Validation数据--使数据真实

数据校验是一个项目的基础模块,也许我们一些入门编码没有多久,了解前端的同学会说,我们已经在前端对数据进行了基础的校验了,还需要在后台对数据进行校验?答案是肯定的,因为前端传递给后台的数据没有百分百值得信任的,这是因为一些别有用心的人,可以模拟前端对后台进行数据发送,可能会发送一些操作后台的指令等非法数据,如果这些数据一旦发送到后台那么后果是很严重的。因此后端校验也是必须的,本章节我们介绍SpringBoot的后端数据校验。

1.SpringBoot校验实现

在SpringBoot2.3以前spring-boot-starter-web自带了validation,并且该模块也提供了相对应的数据绑定功能,但是到了springboot2.3以后就变成了以下依赖进行数据校验

1.1添加依赖

  <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-validation</artifactId>
   </dependency>

1.2 实体注解

@Data
public class Teacher implements Serializable {
 @NotBlank(message = "用户名不可为空")
 private String name;
 @Min(value = 22,message = "年龄不可小于22")
 private int age;
 @Email(message = "邮箱格式错误")
 private String email;
}

1.3 Controller层的实现

@Slf4j
@Controller
public class TeacherController {
 @RequestMapping("/index")
 public String index( Teacher teacher,Model model) {
   teacher = new Teacher();
   teacher.setName("张三");
   teacher.setAge(28);
   teacher.setEmail("1359282905@qq.com");
   model.addAttribute("teacherInfo",teacher);
   return "index";
 }
 @RequestMapping(value = "/add", method = RequestMethod.POST)
 public String add(@ModelAttribute("teacherInfo") @Validated Teacher teacher, BindingResult rs) {
   System.out.println(rs);
   if (rs.hasErrors()) {
     for (ObjectError error : rs.getAllErrors()) {
       log.debug(error.getDefaultMessage());
     }
   }
   return "index";
 }
}

1.4 Thymeleaf前端的实现

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <title th:text="${teacherInfo.name}">xx</title>
</head>
<body>
<h1>信息</h1>
<div>
 姓名:<label th:utext="${teacherInfo.name}"></label><br/>
 年龄:<label th:utext="${teacherInfo.age}"></label> <br/>
 邮箱:<label th:utext="${teacherInfo.email}"></label><br/>
</div>
<h1>表单提交</h1>
<!-- 表单提交用户信息,注意字段的设置,直接是*{} -->
<form action="#" th:action="@{/add}" th:object="${teacherInfo}" method="post">
 <div><span>姓名</span><input type="text" th:field="*{name}" /> <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></span></div>
 <div><span>年龄</span><input type="text" th:field="*{age}" /> <span th:if="${#fields.hasErrors('age')}" th:errors="*{age}"></span></div>
 <div><span>邮箱</span><input type="text" th:field="*{email}" /> <span th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></span></div>
 <input type="submit" />
</form>
</body>
</html>

访问http://localhost:8080/index然后进行操作:

springboot数据加密解密,springboot数据验证注解

1.5 Thymeleaf前端的实现

注解

说明

@NotNull

限制必须不为null

@NotEmpty

验证注解的元素值不为 null 且不为空(字符串长度不为0、集合大小不为0)

@NotBlank

验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格

@Pattern(value)

限制必须符合指定的正则表达式

@Size(max,min)

限制字符长度必须在 min 到 max 之间(也可以用在集合上)

@Email

验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式

@Max(value)

限制必须为一个不大于指定值的数字

@Min(value)

限制必须为一个不小于指定值的数字

@DecimalMax(value)

限制必须为一个不大于指定值的数字

@DecimalMin(value)

限制必须为一个不小于指定值的数字

@Null

限制只能为null(很少用)

@AssertFalse

限制必须为false (很少用)

@AssertTrue

限制必须为true (很少用)

@Past

限制必须是一个过去的日期

@Future

限制必须是一个将来的日期

@Digits(integer,fraction)

限制必须为一个小数,且整数部分的位数不能超过 integer,小数部分的位数不能超过 fraction (很少用)

@CreditCardNumber

信用卡号进行一个大致的验证

@Length(min,max)

检查字段的长度是否在min和max之间,只限于字符串

@URL(protocol,host,port

检查是否为一个有效的URL,如果提供了protocol,host等,则该URL满足条件