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等注解进行相应的校验。