本文將全面的介紹如何使用 validator 進(jìn)行數(shù)據(jù)校驗(yàn)
本文源碼: https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/springboot-validate
準(zhǔn)備工作
我們只需要引入 spring-boot-starter-web
包即可使用
1.常用注解
常用注解
2.簡(jiǎn)單的實(shí)體校驗(yàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class carddto { @notblank private string cardid; @size (min = 10 , max = 10 ) @notnull private string cardnum; // 卡號(hào) @past @notnull private date createdate; @range (max = 3 ) private string cardtype; // 省略get set } |
1
2
3
4
5
6
7
8
9
|
@restcontroller public class usercontroller { @postmapping ( "simple" ) public object simple( @requestbody @valid carddto carddto) { return carddto; } } |
- 實(shí)體屬性上添加校驗(yàn)注解
- controller 方法 參數(shù)前 使用@valid 即可
3. 復(fù)雜的實(shí)體校驗(yàn)
3.1 嵌套實(shí)體校驗(yàn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class userdto { @notblank private string userid; @notblank private string username; private string password; @valid private list<carddto> cardlist; //省略 get set } |
controller 寫法 同上,只是在 userdto cardlist 屬性上標(biāo)記@valid 注解 即可。 3.2 list<dto> 校驗(yàn)
無效示例
如果我們想校驗(yàn) 一個(gè)實(shí)體list,如上圖所示的這種寫法是完全不起效的。
我們需要像 嵌套校驗(yàn) 時(shí)一樣,對(duì) list<carddto>
做一層封裝
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class validlist<e> implements list<e> { @valid private list<e> list = new arraylist<>(); public list<e> getlist() { return list; } public void setlist(list<e> list) { this .list = list; } // 省略了 實(shí)現(xiàn)方法 } |
重寫實(shí)現(xiàn)方法完全使用 this.list.xxx()
gitee:spring 會(huì)將數(shù)據(jù)封裝到我們定義的 list 屬性中,又將屬性聲明了 @valid 使得 hibernate validator 可以為我們做校驗(yàn)!
3.3 使用 @validated 分組校驗(yàn)
1
2
3
4
5
|
public interface insert { } public interface update { } |
定義兩個(gè)接口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class groupcarddto { @notblank (groups = {update. class }) private string id; @notblank (groups = {insert. class }) private string cardnum; @notnull (groups = {insert. class , update. class }) private integer cardtype; //省略 get set } |
實(shí)體標(biāo)記的注解中添加 group 屬性
1
2
3
4
|
@postmapping ( "insert_card" ) public object insert_card( @requestbody @validated (insert. class ) groupcarddto card){ return card; } |
使用 @validated(xxx.class) 標(biāo)記參數(shù),完成分組校驗(yàn)!
4.自定義注解校驗(yàn)
當(dāng) validator 提供的注解無法滿足我們的業(yè)務(wù)需求,可以通過自定義的方式來實(shí)現(xiàn)校驗(yàn)。
需求:校驗(yàn)?zāi)匙址仨殲榇髮懟蛘咝?/p>
1
2
3
4
|
public enum casemode { upper, lower } |
定義一個(gè)枚舉類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import javax.validation.constraint; import javax.validation.payload; import java.lang.annotation.*; @target ( { elementtype.field }) @retention (retentionpolicy.runtime) @constraint (validatedby = checkcasevalidator. class ) @documented public @interface checkcase { string message() default "" ; class <?>[] groups() default {}; class <? extends payload>[] payload() default {}; casemode value() default casemode.lower; } |
- 定義注解
- @constraint 指定我們的校驗(yàn)邏輯實(shí)現(xiàn)類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import javax.validation.constraintvalidator; import javax.validation.constraintvalidatorcontext; public class checkcasevalidator implements constraintvalidator<checkcase, string> { private casemode casemode; @override public void initialize(checkcase constraintannotation) { this .casemode = constraintannotation.value(); } @override public boolean isvalid(string value, constraintvalidatorcontext context) { if (value == null || "" .equals(value.trim())) { return false ; } switch ( this .casemode) { case lower: return value.equals(value.tolowercase()); case upper: return value.equals(value.touppercase()); default : return false ; } } } |
- initialize() 初始化時(shí)執(zhí)行,可以用來獲取注解中的屬性
- isvalid() 實(shí)現(xiàn)我們的校驗(yàn)邏輯
備注
我們自定義的注解依然支持 @validated group 分組
本節(jié)源碼: https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/springboot-validate
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/39279c025b15