本文將全面的介紹如何使用 validator 進行數(shù)據(jù)校驗
本文源碼: https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/springboot-validate
準備工作
我們只需要引入 spring-boot-starter-web
包即可使用
1.常用注解
常用注解
2.簡單的實體校驗
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; // 卡號 @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; } } |
- 實體屬性上添加校驗注解
- controller 方法 參數(shù)前 使用@valid 即可
3. 復雜的實體校驗
3.1 嵌套實體校驗
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 屬性上標記@valid 注解 即可。 3.2 list<dto> 校驗
無效示例
如果我們想校驗 一個實體list,如上圖所示的這種寫法是完全不起效的。
我們需要像 嵌套校驗 時一樣,對 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; } // 省略了 實現(xiàn)方法 } |
重寫實現(xiàn)方法完全使用 this.list.xxx()
gitee:spring 會將數(shù)據(jù)封裝到我們定義的 list 屬性中,又將屬性聲明了 @valid 使得 hibernate validator 可以為我們做校驗!
3.3 使用 @validated 分組校驗
1
2
3
4
5
|
public interface insert { } public interface update { } |
定義兩個接口
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 } |
實體標記的注解中添加 group 屬性
1
2
3
4
|
@postmapping ( "insert_card" ) public object insert_card( @requestbody @validated (insert. class ) groupcarddto card){ return card; } |
使用 @validated(xxx.class) 標記參數(shù),完成分組校驗!
4.自定義注解校驗
當 validator 提供的注解無法滿足我們的業(yè)務需求,可以通過自定義的方式來實現(xiàn)校驗。
需求:校驗某字符串必須為大寫或者小寫
1
2
3
4
|
public enum casemode { upper, lower } |
定義一個枚舉類
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 指定我們的校驗邏輯實現(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() 初始化時執(zhí)行,可以用來獲取注解中的屬性
- isvalid() 實現(xiàn)我們的校驗邏輯
備注
我們自定義的注解依然支持 @validated group 分組
本節(jié)源碼: https://gitee.com/yintianwen7/taven-springboot-learning/tree/master/springboot-validate
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/39279c025b15