1. 聲明:
彙總表主要是小編的使用後總結,而且這些註解主要用於字段的校驗。官方文檔
2. 常用的註解彙總表:
註解 | 適用的字段數據類型 | 使用說明 |
---|---|---|
@AssertFalse | Boolean, boolean. | 該註解驗證值爲false的元素,或者說要求驗證元素的值爲false. |
@AssertTrue | Boolean, boolean. | 該註解驗證值爲true的元素,或者說要求驗證元素的值爲true. |
@DecimalMax(value = "max", message = "msg") | BigDecimal,BigInteger,String,Byte,short,int,long and the any sub-type of Number and charSequence. | 該註解要求驗證元素的值在max範圍內即集合表示爲[Targetalue,max] |
@DecimalMin(value = "min", message = "msg") | BigDecimal,BigInteger,String,Byte,short,int,long and the any sub-type of Number and charSequence. | 該註解要求驗證元素的值在min範圍外即集合表示爲[min,Targetalue] |
@Max(value=max) | 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值,即值的區間表示爲[TargetValue,max] |
@Min(value=min) | 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,TargetValue] |
@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. |
驗證註解的元素值的整數位數和小數位數上限,即當變量 target使用該校驗註解時 @Digits(integer = 3,fraction = 3),即要滿足target = 123.1/123.12/123.123 |
@Size(min=最小值, max=最大值) | String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence. | 驗證註解的元素值的在區間[min,max]範圍之內,如字符長度、集合大小 |
@Range(min=最小值, max=最大值) | CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types | 驗證註解的元素值在區間[最小值,最大值]範圍內 |
@Length(min=下限, max=上限) | CharSequence | 驗證註解的元素值長度在[min,max]區間內 |
@NotBlank | CharSequence | 驗證註解的元素值不爲空(不爲null,也不能是空格),@NotBlank只應用於字符串且在校驗時去除字符串中的空格 |
@NotEmpty | CharSequence,Collection , Map and Arrays |
驗證註解的元素值不爲null且不爲空(字符串長度不爲0即可以爲單個空格或者多空格、集合大小不爲0) |
@NotNull | Any type | 驗證註解的元素值只是不能爲null(注,可以爲任意長度的空格) |
@Null | Any type | 驗證註解的元素值必須爲null |
@Future | ava.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant. | 驗證註解的元素值,即日期要比當前時間之後 |
@Past | ava.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant. | 驗證註解的元素值,即日期要比當前時間之前 |
CharSequence | 驗證註解的元素值是Email,也可以通過正則表達式和flag指定自定義的email格式 | |
@Pattern(regex=正則表達式, flag=) | String. Additionally supported by HV: any sub-type of CharSequence. | 驗證註解的元素值與指定的正則表達式匹配 |
@Valid | Any non-primitive type(引用類型) |
驗證關聯的對象,如賬戶對象裏有一個訂單對象,指定驗證訂單對象
|
3. 補充說明@Valid註解之驗證關聯對象:
代碼示例:
package com.test.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.validation.Valid;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
@Data
@Accessors(chain = true)
public final class AccountDTO {
@NotBlank(message = "Missing Mandatory Field")
String id;
@NotBlank(message = "Missing Mandatory Field")
String merchantReference;
@NotBlank(message = "Missing Mandatory Field")
String memberId;
@NotBlank(message = "Missing Mandatory Field")
String merchantId;
@NotBlank(message = "Missing Mandatory Field")
String type;
@NotNull(message = "Time Format Not Allowed")
String date;
@Valide
TestLocation location;
@Valid
TestValue value;
@NotBlank(message = "Missing Mandatory Field")
String data;
@NotBlank(message = "Missing Mandatory Field")
String dataType;
@Valid
TestEvent event;
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public static class TestLocation {
@NotBlank(message = "Missing Mandatory Field")
String id;
@DecimalMin(value = "0", message = "Value Not Allowed")
@DecimalMax(value = "180", message = "Value Not Allowed")
BigDecimal lat;
@DecimalMin(value = "0", message = "Value Not Allowed")
@DecimalMax(value = "90", message = "Value Not Allowed")
BigDecimal lng;
}
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public static class TestValue {
@DecimalMin(value = "0", message = "Value Not Allowed")
BigDecimal amount;
@NotBlank(message = "Missing Mandatory Field")
String type;
}
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public static class TestEvent {
@NotBlank(message = "Missing Mandatory Field")
String name;
@Valid
Map<String, String> data;
}
public void setDate(String date) {
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+HH:mm");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
sdf.parse(date);
this.date = date;
} catch (ParseException e) {
e.printStackTrace();
this.date = null;
}
}
}
根據代碼示例顯示,可以得出一下結論:
1.TestDTO類中包含了一些基本類型的字段,如:id ,type等String類型字段。針對這些類型字段我們可以直接使用如:@NotNull,@NotEmpty,@NotBlank等註解進行校驗。
2.此外TestDTO類中還包含了一些內聯對象,即其引用類型的字段,如:TestValue,TestEvent,TestLocation等類的引用類型字段。除此之外,這些類中同時包含了一些基本類型的字段,如:id ,name等String類型字段。當我們要校驗這些String類型字段時,我們需要在TestValue,TestEvent,TestLocation等類的引用類型字段上先加上@Valid註解,然後在id ,name等String類型字段上加上@NotNull,@NotEmpty,@NotBlank等註解進行相應的校驗。