springmvc支持的數據校驗是jsr303的標準,通過在bean的屬性上打上@notnull、@max等進行驗證。jsr303提供有很多annotation接口,而springmvc對于這些驗證是使用hibernate的實現,所以我們需要添加hibernate的一個validator包:
依賴引用
1
2
|
compile 'javax.validation:validation-api:2.0.0.final' compile 'org.hibernate:hibernate-validator:6.0.0.final' |
框架已經提供校驗如下:
jsr提供的校驗注解:
@null 被注釋的元素必須為 null
@notnull 被注釋的元素必須不為 null,不能為 null , 可以為 ""
@asserttrue 被注釋的元素必須為 true
@assertfalse 被注釋的元素必須為 false
@min(value) 被注釋的元素必須是一個數字,其值必須大于等于指定的最小值
@max(value) 被注釋的元素必須是一個數字,其值必須小于等于指定的最大值
@decimalmin(value) 被注釋的元素必須是一個數字,其值必須大于等于指定的最小值
@decimalmax(value) 被注釋的元素必須是一個數字,其值必須小于等于指定的最大值
@size(max=, min=) 驗證對象(array,collection,map,string)長度是否在給定的范圍之內
@digits (integer, fraction) 被注釋的元素必須是一個數字,其值必須在可接受的范圍內
@past 被注釋的元素必須是一個過去的日期
@future 被注釋的元素必須是一個將來的日期
@pattern(regex=,flag=) 被注釋的元素必須符合指定的正則表達式
hibernate validator提供的校驗注解:
@notblank(message =) 只能作用在string上,不能為null,而且調用trim()后,長度必須大于0
@email 被注釋的元素必須是電子郵箱地址
@length(min=,max=) 被注釋的字符串的大小必須在指定的范圍內
@notempty 被注釋的字符串的必須非空,不能為 null、"",可以為 " "
@range(min=,max=,message=) 被注釋的元素必須在合適的范圍內
實例演示
創建需要被校驗的實體類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.yiba.wifi.news.bean.model; import org.hibernate.validator.constraints.length; import javax.validation.constraints.*; public class user { @notblank (message = "用戶名不能為null,長度必須大于0" ) string name; //用戶名 @min (value = 1 , message = "最小年齡為1歲" ) @max (value = 120 , message = "最大年齡為120歲" ) integer age; //年齡 @email (message = "郵箱格式錯誤" ) @notblank (message = "郵箱格式錯誤" ) string email; //郵箱 @length (min = 6 , max = 12 , message = "密碼長度必須在6位到12位之間" ) string pwd; //密碼 //get、set......... } |
注意在校驗郵箱的時候,當 email 為 "", 或者 null 的時候,會通過 @email驗證,所以郵箱校驗需要 @email和 @notblank 共同起作用。
controller 接口設計,在參數接受的地方添加 @validated 關鍵字
1
2
3
4
5
6
7
8
|
/** * 登錄接口 * @return */ @postmapping ( "login" ) public string login( @validated @requestbody user user) { return "ok" ; } |
訪問測試:
當訪問數據是如下格式的時候
1
2
3
4
5
6
|
{ "name" : "" , "age" : 0 , "email" : "" , "pwd" : "" } |
響應為:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
{ "timestamp" : 1524640724522 , "status" : 400 , "error" : "bad request" , "exception" : "org.springframework.web.bind.methodargumentnotvalidexception" , "errors" : [ { "codes" : [ "notblank.user.email" , "notblank.email" , "notblank.java.lang.string" , "notblank" ], "arguments" : [ { "codes" : [ "user.email" , "email" ], "arguments" : null , "defaultmessage" : "email" , "code" : "email" } ], "defaultmessage" : "郵箱格式錯誤" , "objectname" : "user" , "field" : "email" , "rejectedvalue" : "" , "bindingfailure" : false , "code" : "notblank" }, { "codes" : [ "notblank.user.name" , "notblank.name" , "notblank.java.lang.string" , "notblank" ], "arguments" : [ { "codes" : [ "user.name" , "name" ], "arguments" : null , "defaultmessage" : "name" , "code" : "name" } ], "defaultmessage" : "用戶名不能為null,長度必須大于0" , "objectname" : "user" , "field" : "name" , "rejectedvalue" : "" , "bindingfailure" : false , "code" : "notblank" }, { "codes" : [ "length.user.pwd" , "length.pwd" , "length.java.lang.string" , "length" ], "arguments" : [ { "codes" : [ "user.pwd" , "pwd" ], "arguments" : null , "defaultmessage" : "pwd" , "code" : "pwd" }, 12 , 6 ], "defaultmessage" : "密碼長度必須在6位到12位之間" , "objectname" : "user" , "field" : "pwd" , "rejectedvalue" : "" , "bindingfailure" : false , "code" : "length" }, { "codes" : [ "min.user.age" , "min.age" , "min.java.lang.integer" , "min" ], "arguments" : [ { "codes" : [ "user.age" , "age" ], "arguments" : null , "defaultmessage" : "age" , "code" : "age" }, 1 ], "defaultmessage" : "最小年齡為1歲" , "objectname" : "user" , "field" : "age" , "rejectedvalue" : 0 , "bindingfailure" : false , "code" : "min" } ], "message" : "validation failed for object='user'. error count: 4" , "path" : "/yiba/sms/login" } |
可以看到本地請求,4個字段校驗都沒通過,那么我有沒有辦法獲取異常信息呢,答案是有的,需要我們修改 controller 接口。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/** * 登錄接口 * * @return */ @postmapping ( "login" ) public string login( @validated @requestbody user user, bindingresult bindingresult) { if (bindingresult.haserrors()) { //有校驗沒通過 list<objecterror> errorlist = bindingresult.getallerrors(); for (objecterror error : errorlist) { system.out.println(error.getdefaultmessage()); //輸出具體的錯誤信息 } return "參數異常" ; } return "ok" ; } |
再次請求,請求格式如下
1
2
3
4
5
6
|
{ "name" : "" , "age" : 0 , "email" : "" , "pwd" : "" } |
響應如下
參數異常
在控制臺打印的信息如下:
用戶名不能為null,長度必須大于0
密碼長度必須在6位到12位之間
最小年齡為1歲
郵箱格式錯誤
可以看到我們已經正常的獲取到了校驗信息了。
下面我們來做一次參照正確的訪問:
請求參數如下:
1
2
3
4
5
6
|
響應如下:
ok
控制臺什么也沒輸出。
總結
以上所述是小編給大家介紹的spring請求參數校驗功能實例演示,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/zhaoyanjun/p/9007056.html