@Size 注解用于约束字段的长度必须在指定范围内。支持的类型有:
CharSequence (评估字符序列的长度)
Collection(评估集合大小)
Map(评估 Map 大小)
数组(评估数组长度)
注意:如果被 @Size 修饰的字段为 null,则约束不起作用。
@Size 注解源码如下:
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented @Constraint(validatedBy = { }) public @interface Size { String message() default "{javax.validation.constraints.Size.message}"; Class<?>[] groups() default { }; Class<? extends Payload>[] payload() default { }; // 元素的大小必须大于或等于该值 int min() default 0; // 元素的大小必须小于或等于该值 int max() default Integer.MAX_VALUE; // 在同一元素上定义多个 @Size 注解 @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented @interface List { Size[] value(); } }
注意:message、groups、payload 属性用法参考 @NotNull 注解。
使用 @Size 约束字符串的长度,例如:
package com.hx.validation; import lombok.Builder; import lombok.Data; import org.hibernate.validator.HibernateValidator; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.constraints.Size; import java.util.Set; /** * 注解元素(Array,Collection,Map,String)长度是否在给定的范围之内 * @author hxstrive.com */ @Data @Builder public class SizeStringDemo { @Size(min = 5, max = 10, message = "名称长度范围 5~10") private String name; public static void main(String[] args) { SizeStringDemo.builder().name(null).build().validator("case1"); // OK SizeStringDemo.builder().name("").build().validator("case2"); // Fail SizeStringDemo.builder().name("1234").build().validator("case3"); // Fail SizeStringDemo.builder().name("12345").build().validator("case4"); // OK SizeStringDemo.builder().name("123456").build().validator("case5"); // OK SizeStringDemo.builder().name("1234567890").build().validator("case6"); // OK SizeStringDemo.builder().name("12345678901").build().validator("case7"); // Fail } private void validator(String caseName) { validator(caseName, this); } private <T> void validator(String caseName, T obj) { // 手动调用 API 对定义了注解的字段进行校验 Validator validator = Validation.byProvider(HibernateValidator.class).configure() .failFast(true).buildValidatorFactory().getValidator(); Set<ConstraintViolation<T>> set = validator.validate(obj); if (set.size() > 0) { // 校验失败 System.out.println(caseName + " :: Fail :: " + set.iterator().next().getMessage()); } else { // 校验通过 System.out.println(caseName + " :: Succeed"); } } }
运行示例,输出如下:
case1 :: Succeed case2 :: Fail :: 名称长度范围 5~10 case3 :: Fail :: 名称长度范围 5~10 case4 :: Succeed case5 :: Succeed case6 :: Succeed case7 :: Fail :: 名称长度范围 5~10
使用 @Size 注解约束集合(Set、List)中元素的个数,例如:
package com.hx.validation; import lombok.Builder; import lombok.Data; import org.hibernate.validator.HibernateValidator; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.constraints.Size; import java.util.Arrays; import java.util.List; import java.util.Set; /** * 注解元素(Array,Collection,Map,String)长度是否在给定的范围之内 * @author hxstrive.com **/ @Data @Builder public class SizeCollectionDemo { @Size(min = 2, max = 4, message = "名称列表必须大于等于2,小于等于4") private List<String> name; public static void main(String[] args) { SizeCollectionDemo.builder().name(null).build().validator("case1"); // OK SizeCollectionDemo.builder().name(Arrays.asList()).build().validator("case2"); // Fail SizeCollectionDemo.builder().name(Arrays.asList("1")).build().validator("case3"); // Fail SizeCollectionDemo.builder().name(Arrays.asList("1", "2")).build().validator("case4"); // OK SizeCollectionDemo.builder().name(Arrays.asList("1", "2", "3")).build().validator("case5"); // OK SizeCollectionDemo.builder().name(Arrays.asList("1", "2", "3", "4")).build().validator("case6"); // OK SizeCollectionDemo.builder().name(Arrays.asList("1", "2", "3", "4", "5")).build().validator("case7"); // Fail } private void validator(String caseName) { validator(caseName, this); } private <T> void validator(String caseName, T obj) { // 手动调用 API 对定义了注解的字段进行校验 Validator validator = Validation.byProvider(HibernateValidator.class).configure() .failFast(true).buildValidatorFactory().getValidator(); Set<ConstraintViolation<T>> set = validator.validate(obj); if (set.size() > 0) { // 校验失败 System.out.println(caseName + " :: Fail :: " + set.iterator().next().getMessage()); } else { // 校验通过 System.out.println(caseName + " :: Succeed"); } } }
运行示例,输出如下:
case1 :: Succeed case2 :: Fail :: 名称列表必须大于等于2,小于等于4 case3 :: Fail :: 名称列表必须大于等于2,小于等于4 case4 :: Succeed case5 :: Succeed case6 :: Succeed case7 :: Fail :: 名称列表必须大于等于2,小于等于4
使用 @Size 约束 Map 中元素个数,例如:
package com.hx.validation; import lombok.Builder; import lombok.Data; import org.hibernate.validator.HibernateValidator; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.constraints.Size; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * 注解元素(Array,Collection,Map,String)长度是否在给定的范围之内 * @author hxstrive.com **/ @Data @Builder public class SizeMapDemo { @Size(min = 2, max = 4, message = "名称长度必须大于等于2,小于等于4") private Map<String,String> name; public static void main(String[] args) { SizeMapDemo.builder().name(null).build().validator("case1"); // OK SizeMapDemo.builder().name(new HashMap<>()).build().validator("case2"); // Fail SizeMapDemo.builder().name(getMap("1")).build().validator("case3"); // Fail SizeMapDemo.builder().name(getMap("1", "2")).build().validator("case4"); // OK SizeMapDemo.builder().name(getMap("1", "2", "3")).build().validator("case5"); // OK SizeMapDemo.builder().name(getMap("1", "2", "3", "4")).build().validator("case6"); // OK SizeMapDemo.builder().name(getMap("1", "2", "3", "4", "5")).build().validator("case7"); // Fail } private static Map<String,String> getMap(String...names) { Map<String,String> map = new HashMap<>(); Arrays.stream(names).forEach(name -> { map.put(name, name); }); return map; } private void validator(String caseName) { validator(caseName, this); } private <T> void validator(String caseName, T obj) { // 手动调用 API 对定义了注解的字段进行校验 Validator validator = Validation.byProvider(HibernateValidator.class).configure() .failFast(true).buildValidatorFactory().getValidator(); Set<ConstraintViolation<T>> set = validator.validate(obj); if (set.size() > 0) { // 校验失败 System.out.println(caseName + " :: Fail :: " + set.iterator().next().getMessage()); } else { // 校验通过 System.out.println(caseName + " :: Succeed"); } } }
运行示例,输出如下:
case1 :: Succeed case2 :: Fail :: 名称长度必须大于等于2,小于等于4 case3 :: Fail :: 名称长度必须大于等于2,小于等于4 case4 :: Succeed case5 :: Succeed case6 :: Succeed case7 :: Fail :: 名称长度必须大于等于2,小于等于4
使用 @Size 约束数组,限制数组大小在给定的范围内,例如:
package com.hxstrive.validation; import lombok.Builder; import lombok.Data; import org.hibernate.validator.HibernateValidator; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.constraints.Size; import java.util.Set; /** * 注解元素(Array,Collection,Map,String)长度是否在给定的范围之内 * @author hxstrive.com **/ @Data @Builder public class SizeArrayDemo { @Size(min = 2, max = 4, message = "name数组长度大于等于2,小于等于4") private String[] name; public static void main(String[] args) { SizeArrayDemo.builder().name(null).build().validator("case1"); // OK SizeArrayDemo.builder().name(new String[]{}).build().validator("case2"); // Fail SizeArrayDemo.builder().name(new String[]{"1"}).build().validator("case3"); // Fail SizeArrayDemo.builder().name(new String[]{"1", "2"}).build().validator("case4"); // OK SizeArrayDemo.builder().name(new String[]{"1", "2", "3"}).build().validator("case5"); // OK SizeArrayDemo.builder().name(new String[]{"1", "2", "3", "4"}).build().validator("case6"); // OK SizeArrayDemo.builder().name(new String[]{"1", "2", "3", "4", "5"}).build().validator("case7"); // Fail } private void validator(String caseName) { validator(caseName, this); } private <T> void validator(String caseName, T obj) { // 手动调用 API 对定义了注解的字段进行校验 Validator validator = Validation.byProvider(HibernateValidator.class).configure() .failFast(true).buildValidatorFactory().getValidator(); Set<ConstraintViolation<T>> set = validator.validate(obj); if (set.size() > 0) { // 校验失败 System.out.println(caseName + " :: Fail :: " + set.iterator().next().getMessage()); } else { // 校验通过 System.out.println(caseName + " :: Succeed"); } } }
运行示例,输出如下:
case1 :: Succeed case2 :: Fail :: name数组长度大于等于2,小于等于4 case3 :: Fail :: name数组长度大于等于2,小于等于4 case4 :: Succeed case5 :: Succeed case6 :: Succeed case7 :: Fail :: name数组长度大于等于2,小于等于4