bean validation 中內(nèi)置的 constraint
@null 被注釋的元素必須為 null
@notnull 被注釋的元素必須不為 null
@asserttrue 被注釋的元素必須為 true
@assertfalse 被注釋的元素必須為 false
@min(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須大于等于指定的最小值
@max(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須小于等于指定的最大值
@decimalmin(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須大于等于指定的最小值
@decimalmax(value) 被注釋的元素必須是一個(gè)數(shù)字,其值必須小于等于指定的最大值
@size(max=, min=) 被注釋的元素的大小必須在指定的范圍內(nèi)
@digits (integer, fraction) 被注釋的元素必須是一個(gè)數(shù)字,其值必須在可接受的范圍內(nèi)
@past 被注釋的元素必須是一個(gè)過去的日期
@future 被注釋的元素必須是一個(gè)將來的日期
@pattern(regex=,flag=) 被注釋的元素必須符合指定的正則表達(dá)式
hibernate validator 附加的 constraint
@notblank(message =) 驗(yàn)證字符串非null,且長度必須大于0
@email 被注釋的元素必須是電子郵箱地址
@length(min=,max=) 被注釋的字符串的大小必須在指定的范圍內(nèi)
@notempty 被注釋的字符串的必須非空
@range(min=,max=,message=) 被注釋的元素必須在合適的范圍內(nèi)
效果和優(yōu)點(diǎn)
先看最后效果:
1
2
3
4
5
6
|
public class userentity { @password private string password; @email private string email; } |
上面使用了兩個(gè)自定義的注解來驗(yàn)證password和email,這樣做的好處是:一處定義,處處使用,要修改驗(yàn)證規(guī)則時(shí),也只要修改注解就可以了。而如果自定義,使用hibernate提供的標(biāo)簽的話:
1
2
|
@pattern (regexp= "..." ) private string email; |
如果寫了很多個(gè)類之后,突然要修改驗(yàn)證規(guī)則regexp,此時(shí)工作量將要大得多。
實(shí)現(xiàn)
首先,引入hibernate validation依賴,添加:
1
2
3
4
5
6
7
|
<!-- hibernate validator --> <!-- hibernate 驗(yàn)證框架 --> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-validator</artifactid> <version> 5.2 . 2 . final </version> </dependency> |
hibernate validation是jsr的參考實(shí)現(xiàn),所以,用它做bean驗(yàn)證。
自定義一個(gè)驗(yàn)證注解分為三步:
- 創(chuàng)建注解(create a constraint annotation)
- 創(chuàng)建驗(yàn)證類(implement a validator)
- 定義默認(rèn)錯(cuò)誤信息(define a default error message)
第一步,創(chuàng)建注解:
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
|
@target ({ method, field, annotation_type, constructor, parameter }) @retention (runtime) @documented @constraint (validatedby = { emailvalidator. class }) public @interface email { string message() default "這不是有效的電子郵件格式" ; /** * @return the regular expression to match */ string regexp() default "[a-za-z0-9._%+-]+@[a-za-z0-9]+\\.[a-za-z]{2,4}" ; class <?>[] groups() default { }; class <? extends payload>[] payload() default { }; /** * defines several {@link size} annotations on the same element. * * @see size */ @target ({ method, field, annotation_type, constructor, parameter }) @retention (runtime) @documented @interface list { email[] value(); } } |
通過@interface關(guān)鍵字來創(chuàng)建注解,而每一個(gè)方法就是注解的一個(gè)參數(shù)。比如上面的代碼,就可以這樣使用@email(regexp="...",message="...")
。其它可以不用去管,直接復(fù)制就可以了,要注意的是@constraint(validatedby = { emailvalidator.class })
,這里指定注解的驗(yàn)證類,根據(jù)實(shí)際替換類名。
第二步,創(chuàng)建驗(yàn)證類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class emailvalidator implements constraintvalidator<email, string>{ private string regexp; @override public void initialize(email constraintannotation) { this .regexp = constraintannotation.regexp(); } @override public boolean isvalid(string value, constraintvalidatorcontext context) { if (value== null ){ return true ;} if ( value.matches(regexp)){ return true ; } return false ; } } |
這里只要實(shí)現(xiàn)constraintvalidator<email, string>
接口就創(chuàng)建了一個(gè)驗(yàn)證器。initialize方法得到注解的regexp值,在isvalid方法中進(jìn)行驗(yàn)證,符合正則表達(dá)式就返回true,否則返回false。
需要注意的是,當(dāng)value為空,也就是驗(yàn)證的對象沒有初始化的時(shí)候,要編寫相應(yīng)的驗(yàn)證規(guī)則,不然會(huì)報(bào)錯(cuò)的。在上面代碼中編寫的是:
1
|
if (value== null ){ return true ;} |
也即是,當(dāng)驗(yàn)證對象為空時(shí),返回成功。
第三步是編寫默認(rèn)錯(cuò)誤信息。其實(shí)這一步在第一步已經(jīng)做了,通過default,所以這步不用做。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:https://blog.csdn.net/ruangong1203/article/details/51002360