SpringBoot參數(shù)校驗示例詳解
SpringBoot自帶了validation工具可以從后端對前端傳來的參數(shù)進行校驗,用法如下:
首先得添加依賴
<!-- 參數(shù)校驗 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
一、簡單數(shù)據(jù)類型校驗
1.1 編寫相關(guān)Controller
@Validated
@RestController
public class TestController {
@GetMapping("/t1")
// name不能為空
public String t1(@NotBlank String name){
System.out.println(name);
return name;
}
}
在要開啟參數(shù)校驗的類上方添加Validated注解,代表該類開啟參數(shù)校驗,訪問http://localhost:8080/t1,發(fā)現(xiàn)當(dāng)沒有傳來參數(shù)時,會拋出 ConstraintViolationException 異常。 接下來讓我們測試一下啪
1.2 測試結(jié)果

可以看到不能為空,表示t1路徑下第一個參數(shù)不能為空,接下來我們輸入?yún)?shù)看看

如果我們不想輸出這種提示信息,我們還可以自定義提示信息,這時就要在注解添加message屬性,屬性值代表錯誤信息
public String t1(@NotBlank(message = "名字不能為空") String name){
System.out.println(name);
return name;
}
再運行看看

這樣就正常了。
二、異常處理
好嘍,在上面我們可以看到異常頁面不太好看,這樣我們可以配置自定義的程序出現(xiàn)錯誤跳轉(zhuǎn)的錯誤頁面。
當(dāng)拋出 ConstraintViolationException 異常后,我們可以使用SpringMVC的異常處理器,也可以使用SpringBoot自帶的異常處理機制。
當(dāng)程序出現(xiàn)了異常,SpringBoot會使用自帶的 BasicErrorController 對象處理異常。該處理器會默認跳轉(zhuǎn)到/resources/templates/error.html頁面。
2.1 錯誤頁面
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>錯誤頁面</title>
</head>
<body>
<h1>服務(wù)器開小差了~</h1>
</body>
</html>
2.2 測試結(jié)果

OK,可以正常顯示錯誤頁面
三、參數(shù)校驗相關(guān)注解
參數(shù)校驗相關(guān)注解
| 注解 | 作用 |
|---|---|
| @NotNull | 判斷包裝類是否為null |
| @NotBlank | 判斷字符串是否為null或者是空串(去掉首尾空格) |
| @NotEmpty | 判斷集合是否為空 |
| @Length | 判斷字符的長度(最大或者最小) |
| @Min | 判斷數(shù)值最小值 |
| @Max | 判斷數(shù)值最大值 |
| 判斷郵箱是否合法 |
3.1 測試Controller
@RequestMapping("/t2")
public String t2(@NotBlank @Length(min = 1,max = 5) String name,
@NotNull @Min(0) @Max(150) Integer age,
@NotEmpty @RequestParam List<String> address,
@NotBlank @Email String email){
System.out.println(name);
System.out.println(age);
System.out.println(address);
System.out.println(email);
return "請求成功";
}OK,我們這個測試方法主要就是有四個參數(shù),第一個name不能為空,且長度最小為1且不能超過5,第二個參數(shù)是年齡age,最小值為0且不能超過150, 第三個參數(shù)值就是address,這是一個集合,不能為空,第四個參數(shù)是郵箱,郵箱格式也要正確。
3.2 測試結(jié)果
OK,接下來我們測試一下什么都沒有輸入,直接測試

接下來輸入如下圖

四、對象類型參數(shù)校驗
SpringBoot也可以校驗對象參數(shù)中的每個屬性,用法如下:
4.1 添加實體類
package com.example.springbootdemo4.pojo;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class Student {
@NotBlank(message = "姓名不能為空")
private String name;
@NotNull(message = "年齡不能沒有") @Min(6) @Max(18)
private Integer age;
public Student() {
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [" +
"name='" + name + '\'' +
", age=" + age +
" ]";
}
}4.2 添加測試方法
@RequestMapping("/t3")
public String t3(@Validated Student student, BindingResult brs){
// 判斷是否有參數(shù)異常
if(brs.hasErrors()){
// 所有參數(shù)異常
List<ObjectError> list = brs.getAllErrors();
// 遍歷參數(shù)異常輸出信息
list.forEach(System.out::println);
return "參數(shù)異常";
}
System.out.println(student);
return "success";
}4.3 測試結(jié)果


可以看到控制臺輸出可以識別到,

Ok,這里亂碼是因為這個模塊的JDK版本較高,和項目版本不切合,但是我們可以知道他是表示年齡要在6-18的就好啦

到此這篇關(guān)于SpringBoot參數(shù)校驗示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot參數(shù)校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot @Scope與@RefreshScope注解使用詳解
spring的bean管理中,每個bean都有對應(yīng)的scope。在BeanDefinition中就已經(jīng)指定scope,默認的RootBeanDefinition的scope是prototype類型,使用@ComponentScan掃描出的BeanDefinition會指定是singleton,最常使用的也是singleton2022-11-11
SpringBoot集成Quartz實現(xiàn)定時任務(wù)的方法
Quartz是一個定時任務(wù)框架,其他介紹網(wǎng)上也很詳盡。這里要介紹一下Quartz里的幾個非常核心的接口。通過實例代碼給大家講解SpringBoot集成Quartz實現(xiàn)定時任務(wù)的方法,感興趣的朋友一起看看吧2020-05-05

