SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼
前言
在實(shí)際開發(fā)中,肯定會(huì)經(jīng)常遇到對(duì)參數(shù)字段進(jìn)行校驗(yàn)的場(chǎng)景,雖然大多數(shù)情況下前端都會(huì)進(jìn)行校驗(yàn),但我們知道前端并不可信,所以后臺(tái)也需要進(jìn)行校驗(yàn),通常我們只能寫大量的if else來完成校驗(yàn)工作,而如果使用SpringBoot Validation則可以輕松的通過注解來完成。
環(huán)境配置
引入Jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
使用示例
@Data
@ToString
public class DemoEntity {
// 不能為空,比較時(shí)會(huì)除去空格
@NotBlank(message = "名稱不能為空")
private String name;
// amount必須是一個(gè)大于等于5,小于等于10的數(shù)字
@DecimalMax(value = "10")
@DecimalMin(value = "5")
private BigDecimal amount;
// 必須符合email格式
@Email
private String email;
// size長(zhǎng)度必須在5到10之間
@Size(max = 10, min = 5)
private String size;
// age大小必須在18到35之間
@Min(value = 18)
@Max(value = 35)
private int age;
// user不能為null
@NotNull
private User user;
// 限制必須為小數(shù),且整數(shù)位integer最多2位,小數(shù)位fraction最多為4位
@Digits(integer = 2, fraction = 4)
private BigDecimal digits;
// 限制必須為未來的日期
@Future
private Date future;
// 限制必須為過期的日期
@Past
private Date past;
// 限制必須是一個(gè)未來或現(xiàn)在的時(shí)間
@FutureOrPresent
private Date futureOrPast;
// 支持正則表達(dá)式
@Pattern(regexp = "^\\d+$")
private String digit;
}
注意:請(qǐng)求時(shí),參數(shù)必須加上@Validated才能生效
@RestController
@Slf4j
@RequestMapping("/valid")
public class TestValidController {
@RequestMapping("/demo1")
public String demo12(@Validated @RequestBody DemoEntity demoEntity) {
try {
return "SUCCESS";
} catch (Exception e) {
log.error(e.getMessage(), e);
return "FAIL";
}
}
}
分組
有些時(shí)候,同一個(gè)參數(shù)在不能場(chǎng)景下校驗(yàn)的規(guī)則可能不一樣,這時(shí)候我們就可以通過分組的方式來實(shí)現(xiàn)
實(shí)體類name屬性設(shè)置了兩種校驗(yàn),分別針對(duì)groups為A和B的生效
@NotBlank(message = "名稱不能為空", groups = A.class) @Size(max = 10, min = 5, groups = B.class) private String name;
只要在相對(duì)應(yīng)的接口上選擇A或者B即可
@RestController
@Slf4j
@RequestMapping("/valid")
public class TestValidController {
@RequestMapping("/demo1")
public String demo1(@Validated({A.class}) @RequestBody DemoEntity demoEntity) {
try {
return "SUCCESS";
} catch (Exception e) {
log.error(e.getMessage(), e);
return "FAIL";
}
}
@RequestMapping("/demo2")
public String demo2(@Validated({B.class}) @RequestBody DemoEntity demoEntity) {
try {
return "SUCCESS";
} catch (Exception e) {
log.error(e.getMessage(), e);
return "FAIL";
}
}
}
到此這篇關(guān)于利用SpringBoot Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的文章就介紹到這了,更多相關(guān)SpringBoot Validation數(shù)據(jù)校驗(yàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Struts中使用validate()輸入校驗(yàn)方法詳解
這篇文章主要介紹了Struts中使用validate()輸入校驗(yàn)方法,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09
netty服務(wù)端輔助類ServerBootstrap創(chuàng)建邏輯分析
這篇文章主要介紹了netty服務(wù)端輔助類ServerBootstrap創(chuàng)建邏輯分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼
在實(shí)際開發(fā)中,肯定會(huì)經(jīng)常遇到對(duì)參數(shù)字段進(jìn)行校驗(yàn)的場(chǎng)景,通常我們只能寫大量的if else來完成校驗(yàn)工作,而如果使用SpringBoot Validation則可以輕松的通過注解來完成,接下來小編給大家介紹下利用SpringBoot?Validation快速實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的示例代碼,需要的朋友參考下吧2022-06-06
Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類實(shí)例【附demo源碼下載】
這篇文章主要介紹了Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類,結(jié)合完整實(shí)例形式分析了裝飾者模式實(shí)現(xiàn)圖片的判斷、水印、縮放、復(fù)制等功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09

