Spring常用表單驗證注解


下面是主要的驗證注解及說明:

注解

適用的數據類型

說明

@AssertFalse

Boolean, boolean

驗證注解的元素值是false

@AssertTrue

Boolean, boolean

驗證注解的元素值是true

@DecimalMax(value=x)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

驗證注解的元素值小於等於@ DecimalMax指定的value值

@DecimalMin(value=x)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

驗證注解的元素值小於等於@ DecimalMin指定的value值

@Digits(integer=整數位數, fraction=小數位數)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

驗證注解的元素值的整數位數和小數位數上限

@Future

java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

驗證注解的元素值(日期類型)比當前時間晚

@Max(value=x)

BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number.

驗證注解的元素值小於等於@Max指定的value值

@Min(value=x)

BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number.

驗證注解的元素值大於等於@Min指定的value值

@NotNull

Any type

驗證注解的元素值不是null

@Null

Any type

驗證注解的元素值是null

@Past

java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

驗證注解的元素值(日期類型)比當前時間早

@Pattern(regex=正則表達式, flag=)

String. Additionally supported by HV: any sub-type of CharSequence.

驗證注解的元素值與指定的正則表達式匹配

@Size(min=最小值, max=最大值)

String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence.

驗證注解的元素值的在min和max(包含)指定區間之內,如字符長度、集合大小

@Valid

Any non-primitive type(引用類型)

驗證關聯的對象,如賬戶對象里有一個訂單對象,指定驗證訂單對象

@NotEmpty

CharSequence,CollectionMap and Arrays

驗證注解的元素值不為null且不為空(字符串長度不為0、集合大小不為0)

@Range(min=最小值, max=最大值)

CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types

驗證注解的元素值在最小值和最大值之間

@NotBlank

CharSequence

驗證注解的元素值不為空(不為null、去除首位空格后長度為0),不同於@NotEmpty,@NotBlank只應用於字符串且在比較時會去除字符串的空格

@Length(min=下限, max=上限)

CharSequence

驗證注解的元素值長度在min和max區間內

@Email

CharSequence

驗證注解的元素值是Email,也可以通過正則表達式和flag指定自定義的email格式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM